Java Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 49: Write a Java program to print the following number pattern.
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

Pattern.java

import java.util.Scanner;

public class Pattern
{
	//Method to print the given pattern.
	public static void printPatter(int n)
	{
		for(int i=0; i < n;i++)
		{
			int curr = 1;
			for(int j=0;j<=i;j++)
			{
				System.out.print(curr+" ");
				curr++;
			}
			System.out.println();
		}
		return;	
	}

	//Main Method
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		int n;
		System.out.println("Enter the number of lines to be included in the pattern.");
		n=s.nextInt();

		//Calling the function printPattern.
		printPatter(n);
	}
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.