Search This Blog

Thursday, 21 July 2016

Write an interactive program to print a diamond shape.

import java.util.Scanner;

class HelloWorld
{

    public static void main(String[] args)
 {
 
        int c=0,v=0;
int s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter Number :");
        s = in.nextInt();
     
        for(int i = 1 ;i<=s;i++)
        {
            for(int j=s;j>i;j--)
System.out.print(" ");
for(int k=0;k<i;k++)
System.out.print("* ");
System.out.println();
        }
        for(int i = s-1 ;i>0;i--)
        {
            for(int j=s;j>i;j--)
System.out.print(" ");
for(int k=0;k<i;k++)
System.out.print("* ");
System.out.println();
        }
    }
}


output:
Enter Number:
3
       *
   *      *
 *    *    *
   *      *
       *

No comments:

Post a Comment