import java.util.Scanner;
public class CharacterTriangle
{
 public static void main(String[] args) {
  char ch;
  Scanner sc = new Scanner(System.in);
  System.out.print(“Enter last character of triangle : “);
  ch = sc.next().charAt(0);
  if (ch >= ‘a’ && ch <= ‘z’) {
   ch = (char)(ch – 32);
  }
  for (char r=”A”; r <= ch; r++) {
   for (int sp = ch – r; sp >= 1; sp–) {
    System.out.print(” “);
   }
   for (char c=”A”; c <= r; c++) {
    System.out.print(c);
   }
   for (char c = (char)(r – 1); c >= ‘A’; c–) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter last character of triangle : e

        A
      ABA
    ABCBA
  ABCDCBA
ABCDEDCBA

BUILD SUCCESSFUL (total time: 2 seconds)

LEAVE A REPLY

Please enter your comment!
Please enter your name here