Reverse number triangle program in Java

Reverse number triangle program





import java.util.Scanner;
public class NumberTriangle
{
 public static void main(String[] args) {
  int num;
  Scanner sc = new Scanner(System.in);
  System.out.print(“Enter number of rows : “);
  num = sc.nextInt();
  for (int r = 1; r <= num; r++) {
   for (int sp = r; sp > 1; sp–) {
    System.out.print(” “);
   }
   for (int c = r; c <= num; c++) {
    System.out.print(c);
   }
   for (int c = (num – 1); c >= r; c–) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}


Output:


Enter number of rows : 5


123454321
  2345432
    34543
      454
        5



BUILD SUCCESSFUL (total time: 2 seconds)

LEAVE A REPLY

Please enter your comment!
Please enter your name here