Author Topic: Java Program to display abcd in a diamond shape pattern  (Read 3435 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Java Program to display abcd in a diamond shape pattern
« on: February 10, 2012, 06:28:15 pm »
Here is the Java Program to display abcd in a diamond shape pattern:

      A
    B C D
  E F G H I
J K L M N O P
  Q R S T U
    V W X
      Y



Code: (java)
//Source Code Listing

import java.lang.*;
import java.util.*;
import java.io.*;
 
 
class DisplayABC
{
      
      public static void main(String[] args)
      {
           System.out.println("Display ABC in diamond shape: ");
           int [] shape = new int [] { 1, 3, 5, 7, 5, 3, 1};
           int [] space = new int [] { 6, 4, 2, 0, 2, 4, 6};
           int count = 0;
           int rowindex = 0;
           for(int i = 0; i < 26; i++)
           {
                if(count == 0)
                {
                  int spacecount = space[rowindex];
                  for(int j = 0; j < spacecount; j++)
                     System.out.print(" ");
                }  
                System.out.format("%1$-2c", (char) (i + 'A') );
                count++;
                if(shape[rowindex] == count)
                {
                  System.out.println();
                  rowindex++;
                  count = 0;
                }
           }
      }
}


 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1