In the previous article we have discussed about Java Program to Print the Series 0 2 6 12 20 30 42 … N
In this article we are going to see how to print the series 1 22 333 4444 55555 … N by using Java programming language. How can you write a program to print the following pattern 1 22 333 in java? How do you print this pattern in java 1 22 333 4444 55555 666666 7777777 88888888 999999999? How to write the 1 22 333 pyramid pattern in java? How do you write 1 22 333 4444 pattern in java. Write nested for loops to produce the following output 1 22 333 4444 55555? How to write a program which will produce the following output 1 22 333 4444 55555? And 1 22 333 pattern in c? What is the output of the code Below Print(‘{:,}’.Format(1223334444))? Finally how to print the Print The Following Pattern For The Given N Number Of Rows, Pattern For N = 4 4444 333 22 1?
Java Program to Print the Series 1 22 333 4444 55555 … N
On observing the pattern carefully, we can see
1st number starts from 1 and in each term it will increment by 1 and n times repeated.
1 22 333 4444 55555 . nnnnnnn….n
Let’s see different ways to print the series 1 22 333 4444 55555 … N
Recommended Reading On: 2 … 3 … 6 … 11 … 18 … ? – Java Program to Print the Series 3 6 11 18 27 … N
Method-1: Java Program to Print the Series 1 22 333 4444 55555 … N By Using For Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘n’ which holds the Nth term of the series
- Prompt the user to enter a number as value of n.
- Use a for loop from i=1 to i<=n where the loop is incremented by 1
- Inside the for loop we will again use a for loop from j =1 to j<=i where the loop is incremented by 1
- Inside the j loop we will print the result of i which will be repeated j times.
- Outside j loop we will Print the series separated by single space “ ”.
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(); //for loop to print the series for (int i = 1; i <= n; i++) { for(int j = 1; j<=i; j++) { System.out.print(i); } System.out.print(" "); } } }
Output: Enter the Nth term “N” 5 1 22 333 4444 55555
Method-2: Java Program to Print the Series 1 22 333 4444 55555 … N By Using While Loop
Approach:
- Create Scanner class object.
- Declare an integer variable say ‘n’ which holds the Nth term of the series
- Prompt the user to enter a number as value of n.
- Declare and initialize an integer variable i=1
- Continue a while loop till i<=n, where i is incremented by 1.
- Inside while loop Declare and initialize an integer variable j=1
- Continue a while loop till j<=i, where j is incremented by 1.
- Inside the j loop we will print the result of i which will be repeated j times.
- Outside j loop we will Print the series separated by single space “ ”.
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 i=1; while(i<=n) { int j = 1; while( j<=i) { System.out.print(i); j++; } System.out.print(" "); i++; } } }
Output: Enter the Nth term “N” 6 1 22 333 4444 55555 666666
Method-3: Java Program to Print the Series 1 22 333 4444 55555 … N By Using User Defined Method
Approach:
- The same logic as method 1 but this time we are moving the for inside a user-defined method
- Create a Scanner class object.
- Prompt the user to enter the Nth term “N”
- Call a user defined method printSeries() by passing n as parameter.
- Inside the method use a for loop to find the series.
- Use a for loop from i =1 to i<=n where the loop is incremented by 1
- Inside the for loop we will again use another for loop from j = 1 to j<=i where the loop is incremented by 1
- Inside the j loop we will print the result of i which will be repeated j times.
- Outside j loop we will Print the series separated by single space “ ”.
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) { //for loop to print the series for (int i = 1; i <= n; i++) { for(int j = 1; j<=i; j++) { System.out.print(i); } System.out.print(" "); } } }
Output: Enter the value of Nth term 'N' 10 1 22 333 4444 55555 666666 7777777 88888888 999999999 10101010101010101010
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: