Tuesday 16 February 2016

Java: Draw a Palindromic Pyramid / Triangle java program.

Problem: Palindromic Pyramid

Draw a palindromic pyramid or triangle of a given height.
Sample Input:
5
Sample Output:
        1
      121
    12321
  1234321
123454321

Solve:
Lets see the following Code:
// ADLabs

import java.util.Scanner;

public class NastedForLoop {

 public static void main(String[] args) {

  Scanner sc = new Scanner(System.in);
  System.out.print("Enter any Int Num:");
  int n = sc.nextInt();
  System.out.println();

  // Palindromic Triangle

  for (int i = 1; i <= n; i++) {

   for (int j = i; j < n; j++) {

    System.out.print(" ");
   }

   for (int k = 1; k <= i; k++)
    System.out.print(k);
   for (int l = i - 1; l > 0; l--)
    System.out.print(l);

   System.out.println();

  }

  sc.close();
 }

}




Console:

Enter any Int Num:5

        1
      121
    12321
  1234321
123454321

Happy Coding :)
-@D