Java Program to Print Pyramid of Numbers from user
Example:
import java.util.Scanner;
public class NumbersPyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Number of Rows You Want In Your Pyramid: ");
int noOfRows = sc.nextInt();
int rowCount = 1;
for (int i = noOfRows; i >= 1; i--) {
// print i*2 spaces at starting of each row
for (int j = 1; j <= i*2; j++) {
System.out.print(" ");
}
for (int j = i; j <= noOfRows; j++) {
System.out.print(j+" ");
}
for (int j = noOfRows-1; j >= i; j--) {
System.out.print(j+" ");
}
System.out.println();
rowCount++;
}
}
}
Output:
9 8 9 8 7 8 9 8 7 6 7 8 9 8 7 6 5 6 7 8 9 8 7 6 5 4 5 6 7 8 9 8 7 6 5 4 3 4 5 6 7 8 9 8 7 6 5 4 3 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
Share this page on:
Subscribe Email Updates
to get latest update