Write Half number pyramid program in Java

Half number pyramid program





import java.util.Scanner;
public class HalfNumberPyramid
{
 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 = num; r >= 0; r–) {
   for (int sp = num – r; sp >= 1; sp–) {
    System.out.print(” “);
   }
   for (int c = r; c >= 1; c–) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}


Output:


Enter number of rows : 5


54321
  4321
    321
      21
        1
     

BUILD SUCCESSFUL (total time: 2 seconds)

LEAVE A REPLY

Please enter your comment!
Please enter your name here