Number triangle pyramid program in Java





import java.util.Scanner;
public class NumberTriangle
{
 public static void main(String[] args) 
 {
  int num;
  System.out.print("Enter loop repeat number(rows): ");
  Scanner sc = new Scanner(System.in);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = 1; sp <= num - r; sp++) {
    System.out.print("  ");                         //it is 2 blank space
   }
   for (int c = 1; c <= 2 * r - 1; c++) {
    System.out.print(" " + c);
   }
   System.out.println();
  }
 }
}

Output:

Enter loop repeat number(rows): 5

         1
       1 2 3
     1 2 3 4 5
   1 2 3 4 5 6 7
 1 2 3 4 5 6 7 8 9


BUILD SUCCESSFUL (total time: 4 seconds)

Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my Blog.


No comments:

Post a Comment