Program to print the series 2, 1, 4, 3, 6, 5, …. up to N terms
Given a number N, the task is to print the below pattern upto N terms:
2, 1, 4, 3, 6, 5, …. N terms
Examples:
Input: N = 4
Output: 2, 1, 4, 3
Explanation:
Nth Term = (N % 2 == 0) ? (N - 1) : (N + 1)
1st term = (1 % 2 == 0) ? (1 - 1) : (1 + 1)
= (1 + 1)
= 2
2nd term = (2 % 2 == 0) ? (2 - 1) : (2 + 1)
= (2 - 1)
= 1
3rd term = (3 % 2 == 0) ? (3 - 1) : (3 + 1)
= (3 + 1)
= 4
4th term = (4 % 2 == 0) ? (4 - 1) : (4 + 1)
= (4 - 1)
= 3
Therefore, Series = 2, 1, 4, 3
Input: N = 7
Output: 2, 1, 4, 3, 6, 5, 8
Formula:
Nth Term = (N % 2 == 0) ? (N - 1) : (N + 1)
Below is the solution to above problem:
C++
// C++ program to print the series // 2, 1, 4, 3, 6, 5, …. up to N terms #include <iostream> using namespace std; // Function to print the series void printPattern(int N) { for (int i = 1; i <= N; i++) { // Find and print the ith term cout <<" "<<((i % 2 == 0) ? (i - 1) : (i + 1)); } } // Driver code int main() { // Get the value of N int N = 10; // Print the Series printPattern(N); return 0; } |
chevron_right
filter_none
Java
// Java Program to print the series // 2, 1, 4, 3, 6, 5, …. up to N terms import java.util.*; import java.lang.*; import java.io.*; class GFG { // Function to print the series static void printPattern(int N) { for (int i = 1; i <= N; i++) { // Find and print the ith term System.out.print(" "+((i % 2 == 0) ? (i - 1) : (i + 1))); } } // Driver code public static void main(String args[]) { // Get the value of N int N = 10; // Print the Series printPattern(N); } } |
chevron_right
filter_none
Python3
# Python program to print the series # 2, 1, 4, 3, 6, 5, …. up to N terms # Function to print the series def printPattern(N): for i in range(1, N + 1): # Find and print the ith term print(i - 1 if i % 2 == 0 else i + 1, end = " ") # Driver code N = 10printPattern(N) # This code is contributed by Shrikant13 |
chevron_right
filter_none
C#
// C# program to print the series // 2, 1, 4, 3, 6, 5, …. up to N terms using System; class GFG { // Function to print the series public void printPattern(int N) { for (int i = 1; i <= N; i++) { // Find and print the ith term int a = ((i % 2 == 0) ? (i - 1) : (i + 1)); Console.Write(" {0}", a); } } // Driver code public static void Main() { GFG g = new GFG(); // Get the value of N int N = 10; // Print the Series g.printPattern(N); } } // This code is contributed // by Soumik |
chevron_right
filter_none
PHP
<?php // PHP program to print the series // 2, 1, 4, 3, 6, 5, …. up to N terms // Function to print the series function printPattern($N) { for ($i = 1; $i <= $N; $i++) { // Find and print the ith term echo " " . (($i % 2 == 0) ? ($i - 1) : ($i + 1)); } } // Driver code // Get the value of N $N = 10; // Print the Series printPattern($N); // This code is contributed // by ChitraNayal ?> |
chevron_right
filter_none
Output:
2 1 4 3 6 5 8 7 10 9
Recommended Posts:
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find n terms of Fibonacci type series with given first two terms
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Program to print binomial expansion series
- Program to print Arithmetic Progression series
- Program to print triangular number series till n
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the series 5+55+555+.. up to n terms
- Sum of first n terms of a given series 3, 6, 11, .....
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



