Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms

18 Mar, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 seriesvoid 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 codeint main(){     // Get the value of N    int N = 10;     // Print the Series    printPattern(N);     return 0;}
// 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 seriesstatic 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 codepublic static void main(String args[]){       // Get the value of N    int N = 10;       // Print the Series    printPattern(N);   }}
# Python program to print the series# 2, 1, 4, 3, 6, 5, .... up to N terms # Function to print the seriesdef 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 codeN = 10printPattern(N) # This code is contributed by Shrikant13
// C# program to print the series// 2, 1, 4, 3, 6, 5, .... up to N termsusing System; class GFG{// Function to print the seriespublic 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 codepublic 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
<?php// PHP program to print the series// 2, 1, 4, 3, 6, 5, .... up to N terms // Function to print the seriesfunction 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 SeriesprintPattern($N); // This code is contributed// by ChitraNayal?>
<script>// javascript program to print the series// 2, 1, 4, 3, 6, 5, .... up to N terms // Function to print the seriesfunction printPattern( N){     for (let i = 1; i <= N; i++)    {         // Find and print the ith term        document.write(" " + ((i % 2 == 0) ? (i - 1) : (i + 1)));    }} // Driver code     // Get the value of N    let N = 10;     // Print the Series    printPattern(N);     // This code is contributed by Rajput-Ji </script>
2 1 4 3 6 5 8 7 10 9
 
tufan_gupta2000
SoumikMondal
shrikanth13
ukasp
mayank5326
Rajput-Ji
series
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java