Java Program to Print Palindrome Pyramid Pattern

  • Java program to print palindrome pyramid pattern using for loop.
Sample Output,
     1
    121
   12321
  1234321
 123454321
12345654321

Java program to print palindrome pyramid pattern

package com.tcc.java.programs;

import java.util.Scanner;

public class NumberPyramid {
    public static void main(String[] arg) {
        int rows, i, space, star = 1;
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Number of Rows of Pyramid Pattern");
        rows = scanner.nextInt();

        // printing one row in every iteration
        for (i = 1; i <= rows; i++) {
            // Printing spaces
            for (space = 1; space <= rows - i; space++) {
                System.out.print(" ");
            }
            // Printing stars
            while (star <= (2 * i - 1)) {
                System.out.print(-Math.abs(star - i) + i);
                star++;
            }
            star = 1;
            // move to next row
            System.out.print("\n");
        }
    }
}
Output
Enter Number of Rows of Pyramid Pattern
6
     1
    121
   12321
  1234321
 123454321
12345654321