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

Popular posts from this blog

Practical 12: Refine the student manager program to manipulate the student information from files by using the BufferedReader and BufferedWriter

Practical 9 : Write an interactive program to print a string entered in a pyramid form. For instance, the string “stream” has to be displayed as follows: