Thursday, 19 January 2017

Diamond...

Write a program to print a rectangle with a diamond shape gap exactly at the center of the Rectangle, using an input string with odd numbers of characters, not Exceeding 20 characters.
For Example:-

Input :                             HAPPY-HAPPY
Output:                  Enter a word of odd characters
                             HAPPY-HAPPY

                             HAPPY-HAPPY
                             HAPPY  HAPPY
                             HAPP       APPY
                             HAP            PPY
                             HA                PY
                             H                     Y
                             HA                PY
                             HAP            PPY
                             HAPP       APPY
                             HAPPY  HAPPY

                             HAPPY-HAPPY


Solution:-

import java.util.*;
class diamond
{
     String str;
     void input()
     {
           Scanner sc=new Scanner(System.in);
           System.out.println("Enter a word of odd Characters");
           str=sc.next();
     }
     void generate()
     {
           String space=" ";
           if(str.length()%2!=0)
           {
                String a,b;
                a=str.substring(0,str.length()/2);
                b=str.substring(str.length()/2+1);
                System.out.println("\n"+str);
                int length=a.length();
                int count=1;
                for(int i=0;i<length;i++)
                {
                     String a1=a.substring(0,length-i);
                     String b1=b.substring(i,length);
                     System.out.print(a1);
                     for(int j=1;j<=count;j++)
                     {
                           System.out.print(space);
                     }
                     count=count+2;
                     System.out.println(b1);
                    
                }
                count=count-2;
                for(int i=length-2;i>=0;i--)
                {
                     String a1=a.substring(0,length-i);
                     String b1=b.substring(i,length);
                     System.out.print(a1);
                     count=count-2;
                     for(int j=1;j<=count;j++)
                     {
                           System.out.print(space);
                     }
                     System.out.println(b1);
                    
                }
                System.out.println(str);
           }
           else
           {
                System.out.println("Please Enter a word of odd Characters" );
                System.exit(0);
           }
     }   
     public static void main(String args[])         
     {
           diamond obj=new diamond();
           obj.input();
           obj.generate();
     }
}


No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...