- Java

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
 1 2
 1 2 3
 1 2 3 4

Algorithm:

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.*;</code></pre>
class pattern
{
public static void main(String args[]){
        Scanner scan=new Scanner(System.in);
        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&lt;=n;i++) //Outer loop to count the lines
        {
            for(j=1;j&lt;=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
        }
    }
}

Leave a Reply