Hi All,
I write a program to display the Diamond Shape Asterisk using Java.
import java.util.Scanner;
//main class
public class diamond {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int no = input.nextInt();
diamond access = new diamond();
System.out.println("The shape for this is: ");
access.diamond_res(no);
}
public String diamond_star(int no) //method1
{
if(no>0)
{
return "* " + diamond_star(no-1);
}
else
{
return " ";
}
}
public String diamond_star1(int no)//method2
{
if(no>0)
{
return " * " + diamond_star(no-1);//access method1
}
else
{
return " ";
}
}
public String blankSpace(int no) //method3
{
if(no>0)
{
return " " + blankSpace(no-1);
}
else
{
return " ";
}
}
public void diamond_res(int no)//method4
{
System.out.print(" Output 1.\n\n");
/* for output 1*/
for(int i=1; i<no/2; i++)
{
System.out.print(blankSpace(no-i));//access method3
System.out.println(diamond_star(i));//access method1
}
for(int i=0; i<no/2; i++)
{
System.out.println(diamond_star1(no-i));//access method2
System.out.print(blankSpace(i));//access method3
}
System.out.print(" Output 2.\n\n");
/* for output 2*/
for(int i=1; i<no; i++)
{
System.out.print(blankSpace(no-i));//access method3
System.out.println(diamond_star(i));//access method1
}
for(int i=0; i<no/2; i++)
{
System.out.println(diamond_star1(no-i));//access method2
System.out.print(blankSpace(i));//access method3
}
System.out.print(" Output 3 .\n\n");
/* for output 3*/
for(int i=1; i<no/5; i++)
{
System.out.print(blankSpace(no-i));//access method3
System.out.println(diamond_star(i));//access method1
}
for(int i=0; i<no; i++)
{
System.out.println(diamond_star1(no-i));//access method2
System.out.print(blankSpace(i));//access method3
}
System.out.print(" Output 4 .\n\n");
/* for output 4 */
for(int i=1; i<no; i++)
{
System.out.print(blankSpace(no-i));//access method3
System.out.println(diamond_star(i));//access method1
}
for(int i=0; i<no/5; i++)
{
System.out.println(diamond_star1(no-i));//access method2
System.out.print(blankSpace(i));//access method3
}
System.out.print(" Output 5 .\n\n");
/* for output 5 */
for(int i=1; i<no/5; i++)
{
System.out.print(blankSpace(no-i));
System.out.println(diamond_star(i));
}
for(int i=0; i<no/3; i++)
{
System.out.println(diamond_star1(no-i));
System.out.print(blankSpace(i));
}
System.out.print(" Output 6 .\n\n");
/* for output 6 */
for(int i=1; i<no*2; i++)
{
System.out.print(blankSpace(no-i));
System.out.println(diamond_star(i));
}
for(int i=0; i<no*2; i++)
{
System.out.println(diamond_star1(no-i));
System.out.print(blankSpace(i));
}
}
}
OUTPUT
~~~~~~~~
Enter a number: 10
The shape for this is:
Output 1.
*
* *
* * *
* * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
Output 2.
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
Output 3 .
*
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Output 4 .
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
Output 5 .
*
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
Output 6 .
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
No comments:
Post a Comment