Start Coding Now
Pattern Programsbeginner1 methods

Number Pyramid Pattern in Java

Print a pyramid of numbers where each row contains its row number.

Last updated: 11 January 2026

Method 1: Using Nested Loops

Print row number repeatedly.

public class NumberPyramid {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
}
Output:
    1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5

Explanation

Similar to star pyramid, but print "i " instead of "* ".

Frequently Asked Questions

Try This Program

Copy this code and run it in our free online Java compiler.

Open Java Compiler