Java and C++ and more Programming codes

rclakmal

Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    i saw a question on "YAHOO ANSWERS" and and here is my solution for all of u !!!!!!

    Question was ---
    Code:
    [B][SIZE=2]How to do this java programme?[/SIZE][/B]
    
                       [SIZE=2]Q3. Write a program to draw a diamond sign in to the screen with different heights entered by the user. See following example. 
    
    Enter the height of the diagonal: 7 
         *
        * *
       * * *
      * * * *
       * * *
        * *
         *
    [URL]http://answers.yahoo.com/question/index?qid=20090925215708AAkjRKl[/URL]
    [/SIZE]



    and here is my answer
    Code:
    package diamond;
    import java.util.Scanner;
    
    /**
     *
     * @author lakmal
     */
    class Diamond {
    
        /**
         * @param args the command line arguments
         */
        static int height;
        static char state;
        Diamond(int height){
            for (int i=1;i<=height;i++){
                if (i<=(height/2)){
                for (int j=1;j<=i;j++){
                    System.out.print(" *");
                   
                }
              }
                else{
                    for(int j=1;j<=(height+1-i);j++){
                        System.out.print(" *");
                       
    
                    }
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            // TODO code application logic here
            Scanner input=new Scanner(System.in);
            do{
    
            do{
            System.out.println("Enter your height");
             height=input.nextInt();
            Diamond d1=new Diamond(height);
            } while(height%2!=0);
            System.out.println("Enter a odd number!!");
            System.out.println("Do you want to continue?(y/n)");
            state=input.next().charAt(0);
    
    
    
        } while(state=='y');
    
      }
    }
    Happy coding !!!!!

    For more visit here http://www.elakiri.com/forum/showthread.php?t=245895