print/draw Diamond Shape using Asterisk in java


public class Diamond
{
    public String Diamond_Asterisk(int num)
    {
        if(num>0)
        {
            return "*-" + Diamond_Asterisk(num-1);
        }
        else
        {
            return "-";
        }
    }
     public String Diamond_Asterisk2(int num)
    {
        if(num>0)
        {
            return "-*-" + Diamond_Asterisk(num-1);
        }
        else
        {
            return "-";
        }
    }

    public String Space(int num)
    {
        if(num>0)
        {
            return "-" + Space(num-1);
        }
        else
        {
            return "-";
        }
    }
    public void DiamondResult(int num)
    {
        for(int i=1; i<num; i++)
        {
            System.out.print(Space(num-i));
             System.out.println(Diamond_Asterisk(i));
            
        }
         for(int i=0; i<num; i++)
        {
            System.out.println(Diamond_Asterisk2(num-i));
            System.out.print(Space(i));

        }
     
    }
    

}


//main class


import java.util.Scanner;

public class Main {

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = input.nextInt();

        Diamond access = new Diamond();
        System.out.println("The shape for this is: ");
        access.DiamondResult(num);
        
    }

}
===================================
Sample Output 1: Enter a number: 3 The shape for this is: ---*-- --*-*-- -*-*-*-- --*-*-- ---*-- Sample Output 2: Enter a number: 4 The shape for this is: ----*-- ---*-*-- --*-*-*-- -*-*-*-*-- --*-*-*-- ---*-*-- ----*--

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|