Java Program to Print the Series 1, 12, 123, 1234, …, n

In the previous article, we have discussed about Java Program to Find the Sum of Series 1 + 1 / (1+2) + 1 / (1+2+3) + ……… + 1 / (1+2+3+ … + N)

In this article we are going to see how to print the series 1, 12, 123, 1234, …, n by using Java programming language. Additionally we include from the view of beginners 1 12 123 pattern in java, print 1 12 123 in java, How to print 1 12 123 in java, java program to print 1 12 123, Java program for pattern 1 12 123, 1 12 123 series in java, 1 12 123 pattern in java using for loop, wap to print 1 12 123, 1 12 123 1234 12345 Pattern In Java etc.

Recommended Reading On: Java Program to Print the Series 4 16 36 64 100 …N

Java Program to Print the Series 1, 12, 123, 1234, …, n

On observing the pattern carefully, we can see each term starts from 1 and the last digit of the term is exact same as the nth term.

Example:

Suppose n = 5

Then series: 1, 12, 123, 1234, 12345

Let’s see different ways to print the series 1, 12, 123, 1234, …, n.

Method-1: Java Program to Print the Series 1, 12, 123, 1234, …, n By Using For Loop

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a for loop from i=1 to i<=n (incremented by 1)
  • Inside the for loop, we will again do a for loop from j=1 to j<=i (incremented by 1)
  • Inside that loop we will print the value of j separated by comma.

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 'n' ");
        int n = s.nextInt();
        //for loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            for (int j = 1; j <= i; j++) 
            {
                System.out.print(j);
            }
            System.out.print(", ");
        }             
    }
}
Output:

Enter the value of 'n' 
3
1, 12, 123,

Method-2: Java Program to Print the Series 1, 12, 123, 1234, …, n By Using While Loop

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a while loop from i=1 to i<=n (incremented by 1)
  • Inside the while loop, we will again continue a while loop from j=1 to j<=i (incremented by 1)
  • Inside that loop we will print the value of j separated by comma.

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 'n' ");
        int n = s.nextInt();
        // while loop to print the series
       double sum = 0;
       int i = 1;
       while(i <= n)
       {  
    	    int j = 1;
            while ( j <= i) 
            {
                System.out.print(j);
                j++;
            }
            System.out.print(", ");
            i++;
       }
   }
}
Output:

Enter the value of 'n' 
3
1, 12, 123,

Method-3: Java Program to Print the Series 1, 12, 123, 1234, …, n By Using User Defined Method

Approach:

  • Declare an integer variable say ‘n’ which holds the value of Nth term.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Then call a user defined method say printSeries() by passing n as parameter.
  • Inside method use a for loop from i=1 to i<=n (incremented by 1)
  • Inside the for loop, we will again do a for loop from j=1 to j<=i (incremented by 1)
  • Inside that loop we will print the value of j separated by comma.

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);
   }
    public static void printSeries(int n)
    {
        for (int i = 1; i <= n; i++) 
        {
            for (int j = 1; j <= i; j++) 
            {
                System.out.print(j);
            }
            System.out.print(", ");
        }             
    }
}

Output:

Enter the value of nth term 'n' 
4
1, 12, 123, 1234,

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Solve these applications, it will helps to know analyze yourself how much you have learnt from the information given above.

  1. A Programmer Writes A Program To Print The Following Pattern 1 12 123
  2. A Programmer Writes The Program Given Below 1 12 123
  3. A Programmer Writes The Given Program To Print The Following Pattern On The Screen 1 12 123
  4. Write A Program To Print 1 12 123
  5. Print The Series 1 12 123 1234 12345
  6. 12345 Pattern In Java

Related Java Programs: