Character rhombus shape program in Java



import java.util.Scanner;
public class CharacterRhombus
{
 public static void main(String[] args) {
  char ch;
  System.out.print("Enter any character : ");
  Scanner sc = new Scanner(System.in);
  ch = sc.next().charAt(0);
  if (ch >= 'a' && ch <= 'z') {
   ch = (char)(ch - 32);
  }
  System.out.println();
  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();
  }
  for (char r = 'A';
   'A' <= ch; ch--, r++) {
   for (int sp = r; sp >= 'A'; sp--) {
    System.out.print(" ");
   }
   for (char c = 'A'; c <= ch - 1; c++) {
    System.out.print(c);
   }
   for (char c = (char)(ch - 2); c >= 'A'; c--) {
    System.out.print(c);
   }
   System.out.println();
  }
 }
}

Output:

Enter any character : e

        A
      ABA
    ABCBA
  ABCDCBA
ABCDEDCBA
  ABCDCBA
    ABCBA
      ABA
        A
     

BUILD SUCCESSFUL (total time: 6 seconds)


Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my blog.


No comments:

Post a Comment