Practical 10 : Write an interactive program to print a diamond shape. For example, if user enters the number 3, the diamond will be as follows:
*
* *
* * *
* *
*
Program:
import java.util.Scanner;
public class practical10
{
public static void main(String[] args)
{
int i, j, k;
Scanner scan = new Scanner(System.in);
int no = scan.nextInt();
for (i = 0; i < no; i++)
{
for (j = (no / 2) + 1; j >= i; j--)
{
System.out.print(" ");
}
for (k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
for (i = 0; i < no; i++)
{
System.out.print(" ");
for (j = 0; j <= i; j++)
System.out.print(" ");
for (k = no - 1; k > i; k--)
System.out.print("* ");
System.out.println();
}
}
}
Output:
Comments
Post a Comment