Write the following character triangle pattern in Java




package demo;
import java.util.Scanner;
public class CharacterTriangle
{
 public static void main(String[] args) 
 {
  char r, c, ch, h;
  System.out.println("Enter seed character : ");
  Scanner sc = new Scanner(System.in);
  ch = sc.next().charAt(0);
  if (ch >= 'a' && ch <= 'z') {
   ch = (char)(ch - 32);
  }
  h = ch;
  for (r = 'A'; r <= ch; r++, h--) 
  {
   for (c = 'A'; c <= h; c++) 
   {
    System.out.print(c);
   }
   for (c = (char)(h - 1); c >= 'A'; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

OUTPUT:
run:
Enter seed character : 
e

ABCDEDCBA
ABCDCBA
ABCBA
ABA
A


BUILD SUCCESSFUL (total time: 6 seconds)



No comments:

Post a Comment