Sunday, 25 June 2017

Character pattern of half diamond in Java ~ foundjava


package demo;
import java.util.Scanner;
public class HalfDiamondTriangle
{
 public static void main(String[] args) 

{
  char ch, r, c, p, q, z;
  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);
  }
  System.out.println();
  for (r = 'A', z = ch; r <= ch; r++, z--) {
   for (c = r, p = z; c >= 'A'; c--, p++) {
    System.out.print(p);
   }
   for (c = r, p = (char)(ch - 1); c > 'A'; c--, p--) {
    System.out.print(p);
   }
   System.out.println();
  }
  for (r = 'A', z = (char)(ch - 1); r < ch; r++, z--) {
   for (c = r, p = (char)(r + 1); c < ch; c++, p++) {
    System.out.print(p);
   }
   for (c = z, p = (char)(ch - 1); c > 'A'; c--, p--) {
    System.out.print(p);
   }
   System.out.println();
  }
 }
}

Output:
Enter seed character : 
e

E
DED
CDEDC
BCDEDCB
ABCDEDCBA
BCDEDCB
CDEDC
DED
E

BUILD SUCCESSFUL (total time: 4 seconds)

No comments:

Post a Comment