Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Java Program for Inverted Pyramid Star Pattern​

Printing Inverted Pyramid Star Pattern

In this program we’re going to code inverted pyramid pattern program .

Take a input from user as and give him a message as enter the no of lines and then store it in variable n Then run the for loop start from i=n to i– and then take another inner for loop to take j loop start from j=i to j++ and then print space After that take another inner loop start from j=1 to j++ and then print star

Inverted Pyramid Star Pattern

Algorithm:

  1. Enter the number of lines take input from the user and store it in any variable.(‘n‘ in this case).
  2. Run a loop ‘n’ number of times to iterate through each of the rows. From i=n to i>=1. The loop should be structured as for( i=n; i>=1 : i–).
  3.  Run a nested loop inside the main loop to print the spaces before the pyramid. From j=i to j<n. The loop should be structured as for(j=i;j<n;j++)
  4. Inside this loop print white space.
  5. Run another nested loop after the previous loop to print the stars in each column of a row. From     j=1 to j<2*i-1. The loop should be structured as for(j=1;j<=(2*i-1);j++)
  6. Inside this loop print star.
  7. Move to the next line by printing a new line System.out.println(“”);

Code in Java:

import java.util.Scanner;
public class Pattern1 {

	public static void main(String[] args) {
		int i,j,n;
		Scanner sc = new Scanner(System.in);
	     System.out.println("Enter the no of lines");
	     n = sc.nextInt();
		for(i=n;i>=1;i--)
	     {
	         for(j=i;j<n;j++)
	            System.out.print(" ");
	         
	         for(j=1;j<=(2*i-1);j++)
	            System.out.print("*");
	         
	       System.out.println("");
	     } 
	 }

}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)

One comment on “Java Program for Inverted Pyramid Star Pattern​”