Java program to generate pattern

Java program to generate pattern:

Hi guys....! In this article let's learn how to generate a pattern.Usually a pattern is arrangement of symbols or numbers in a systematic order according to the requirement. In this program let's see how to generate a pattern.

 1 2
 1 2 3
 1 2 3 4


1) Take the input from the user to generate the required pattern.
2) Declare one outer loop to count the lines.
3) Declare the inner loop to generate the proper numbers in each lines.
4) Jump to next line after every execution of inner loop. 

Code goes here:

import java.util.*;

class pattern
public static void main(String args[])
        Scanner scan=new Scanner(;
        int i,j,n;
        System.out.println("Enter the number of lines of pattern to be generated");
        n=scan.nextInt(); //Taking the input from the user to generate the required pattern
        for(i=1;i<=n;i++) //Outer loop to count the lines
            for(j=1;j<=i;j++)// Inner loop to generate the numbers in each line
                System.out.print(" "+j);
            System.out.println(" "); //Jump to next line in a pattern

No comments:

Powered by Blogger.