Java Programs

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

Program 47: Write a Java program to print the following star pattern.
    *
    * *
    * * *
    * * * *
    * * * * *

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++)
		{
			for(int j=0;j<=i;j++)
			{
				System.out.print("* ");
			}
			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.