In the previous article we have discussed about Java Program to Print the Series 10 5 60 15 110 … N
In this article we are going to see how to print the series 2 1 4 3 6 5 …N by using Java programming language.
Java Program to Print the Series 2 1 4 3 6 5 …N
On observing the pattern carefully, we can see
1st & 2nd number is 2, 1 respectively. Then the next number follows a logic
2 1 2+2=4 1+2=3 4+2=6 3+2=5 … and so on.
Example:
| 2 | 1 | 4 | 3 | 6 | 5 | …… | N |
2+2 1+2 4+2 3+2
Let’s see different ways to print the series 2 1 4 3 6 5 …N
Method-1: Java Program to Print the Series 2 1 4 3 6 5 …N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n. - Declare an integer variable say ‘
a’, ‘b’ and initialize it to 2,1 respectively. - Use a for loop
from i=1 to i<=n-2where the loop is incremented by 1 - Inside the for loop we will find the value of
a,baccording to the position ofi. Ifi%2 != 0thena = a+2elseb = b+2 - Print the result in the series.
Program:
import java.util.*;
public class Main
{
public static void main(String [] args)
{
//creating object of Scanner class
Scanner s = new Scanner(System.in);
//Taking input of number of elements in the series
System.out.println("Enter the Nth term “N” ");
int n = s.nextInt();
int a = 2;
int b = 1;
System.out.print(a+" "+b);
//for loop to print the series
for (int i = 1; i <= n-2; i++)
{
if(i%2 != 0)
{
a+=2;
System.out.print(" "+a);
}
else
{
b+=2;
System.out.print(" "+b);
}
}
}
}
Output: Enter the Nth term “N” 5 2 1 4 3 6
Method-2: Java Program to Print the Series 2 1 4 3 6 5 …N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n. - Declare an integer variable say ‘
a’, ‘b’ and initialize it to 2,1 respectively. - Declare and initialize an integer variable
i=1 - Continue a while loop
till i<=n-2, whereiis incremented by 1. - Inside the while loop we will find the value of
a,baccording to the position ofi. Ifi%2 != 0thena = a+2elseb = b+2 - Print the result in the series.
Program:
import java.util.*;
public class Main
{
public static void main(String [] args)
{
//creating object of Scanner class
Scanner s = new Scanner(System.in);
//Taking input of number of elements in the series
System.out.println("Enter the Nth term “N” ");
int n = s.nextInt();
int a = 2, b=1;
System.out.print(a + " " +b);
int i=1;
while(i<=n-2)
{
if(i%2 != 0)
{
a+=2;
System.out.print(" "+a);
}
else
{
b+=2;
System.out.print(" "+b);
}
i++;
}
}
}
Output: Enter the Nth term “N” 7 2 1 4 3 6 5 8
Method-3: Java Program to Print the Series 2 1 4 3 6 5 …N By Using User Defined Method
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘
n’ which holds the number of terms in the series. - Prompt the user to enter a number as value of
n. - Call a user defined method say
printSeries()by passingnas parameter. - Inside method declare an integer variable say ‘
a’, ‘b’ and initialize it to 2,1 respectively. - Use a for loop
from i=1 to i<=n-2where the loop is incremented by 1 - Inside the for loop we will find the value of
a,baccording to the position ofi. Ifi%2 != 0thena = a+2elseb = b+2 - Print the result in the series.
Program:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
// creating object of scanner class
Scanner s = new Scanner(System.in);
//Taking input of number of elements in the series
System.out.println("Enter the value of Nth term 'N' ");
int n = s.nextInt();
// calling printSeries method to print the series
printSeries(n);
}
//printSeries metthod to print the series
public static void printSeries(int n)
{
int a=2, b = 1;
System.out.print(a+" "+b);
//for loop to print the series
for (int i = 1; i <=n-2; i++)
{
if(i%2 != 0)
{
a+=2;
System.out.print(" "+a);
}
else
{
b+=2;
System.out.print(" "+b);
}
}
}
}
Output: Enter the value of Nth term 'N' 9 2 1 4 3 6 5 8 7 10
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Related Java Programs: