Numeric Ascending Pyramid in Java

0 0
Read Time:45 Second

Here’s an solution of how you can create a numeric ascending pyramid in Java:

import java.util.Scanner;

public class NumericAscendingPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of rows for the pyramid: ");
        int rows = scanner.nextInt();

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

This Java program prompts the user to enter the number of rows for the pyramid and then prints a numeric pyramid in ascending order based on the input provided.

For example, if the user enters 5 as the number of rows, the output will be:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

This code uses nested loops to generate the pyramid structure, printing the numbers in ascending order on each row.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %
Verified by MonsterInsights