Java Program to Print the Series 1 2 11 12 21 … N

In the previous article we have discussed about Java Program to Print the Series 2 1 4 3 6 5 N

In this article we are going to see how to print the series 1 2 11 12 21 … N by using Java programming language.

Java Program to Print the Series 1 2 11 12 21 … N

On observing the pattern carefully, we can see

1st & 2nd number is 1, 2 respectively. Then the next number follows a logic

1
2
1+10=11
2+10=12
11+10=21
12+10=22… and so on

Example:

1 2 11 12 21 22 …… N

                                       1+10            2+10          11+10          12+10

Let’s see different ways to print the series 1 2 11 12 21 … N

Method-1: Java Program to Print the Series 1 2 11 12 21 … 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 two integer variables say ‘a’, ‘b’ and initialize it to 1,2 respectively.
  • Use a for loop from i=1 to i<=n-2 where the loop is incremented by 1
  • Inside the for loop we will find the value of a, b according to the position of i. If i%2 != 0 then a = a+10 else b=b+10
  • 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 number of terms ");
        int n = s.nextInt();
        int a = 1;
        int b = 2;
        System.out.print(a+" "+b);
        //for loop to print the series
        for (int i = 1; i <= n-2; i++) 
        {
            if(i%2 != 0)
            {
                a+=10;
                System.out.print(" "+a);
            }
            else
            {
                b+=10;
                System.out.print(" "+b);
            }
        } 
    }
}
Output:

Enter number of terms
5
1 2 11 12 21

Method-2: Java Program to Print the Series 1 2 11 12 21 … 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 two integer variables say ‘a’, ‘b’ and initialize it to 1,2 respectively.
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n-2, where i is incremented by 1.
  • Inside the while loop we will find the value of a, b according to the position of i. If i%2 != 0 then a = a+10 else b=b+10
  • 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 number of terms ");
        int n = s.nextInt();
        int a = 1, b=2;
        System.out.print(a + " " +b);
        int i=1;
        while(i<=n-2)
        {
            if(i%2 != 0)
            {
                a+=10;
                System.out.print(" "+a);
            }
            else
            {
                b+=10;
                System.out.print(" "+b);
            }
            i++;
        }      
    }
}
Output:

Enter number of terms
7 31
1 2 11 12 21 22

Method-3: Java Program to Print the Series 1 2 11 12 21 … 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 passing n as parameter.
  • Inside method declare two integer variables say ‘a’, ‘b’ and initialize it to 1,2 respectively.
  • Use a for loop from i=1 to i<=n-2 where the loop is incremented by 1
  • Inside the for loop we will find the value of a, b according to the position of i. If i%2 != 0 then a = a+10 else b=b+10
  • 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 number of terms ");
        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=1, b = 2;
        System.out.print(a+" "+b);
        //for loop to print the series
        for (int i = 1; i <=n-2; i++) 
        {
            if(i%2 != 0)
            {
                a+=10;
                System.out.print(" "+a);
            }
            else
            {
                b+=10;
                System.out.print(" "+b);
            }
        } 
    }
}
Output:

Enter number of terms
9
1 2 11 12 21 22 31 32 41

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: