Java Programs for Printing Pyramid Patterns of Numbers



Following is a Java program which prints a pyramid of numbers.

Example

 Live Demo

public class PrintingPyramids {
   public static void main(String args[]){
      int n,i,j,k=1;
      n = 5;
      for(i = 0; i<= n; i++) {
         for(j = 0; j<= i; j++){
            System.out.print(k+" ");
            k++;
         }
         System.out.print("\n");
      }
   }
}

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
Updated on: 2019-07-30T22:30:26+05:30

234 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements