COME 2 CODE
Learn by Yourself

Program to Print a Pyramid of given Lines

Hello friends, in this tutorial we are going to write a program to print pyramid of "*" of given lines. So, open your IDE and make a new Project. Now, write the following code in Main.java file.

Main.java :

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    int rows;
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter the number of rows to print : ");
    rows = scanner.nextInt();


    for (int i = 1; i <= rows; i++) {
      for (int j = (rows - i); j >= 1; j--) {
        System.out.print(" ");
      }
      for (int k = 1; k <= i; k++) {
        System.out.print("* ");
      }
      System.out.println();
    }
  }
}

Your output will be like this.

Output :

C:/Users/Username/Desktop>Enter the number of rows you want to print :
5
    *
   * *
  * * *
 * * * *
* * * * *
QUICK LINKS :