Friday 2 October 2015

Star pattern in java programming #3

In the program the following star pattern will be printed.

Diamond pattern:


Click here for Star pattern in java programming #1

Click here for Star pattern in java programming #2

This program will accept a number as input. The loops are used to build a diamond pattern of *.

/* 
* Diamond star pattern
**/
package com.blogspot.java2bigdata;

import java.io.*;
import java.lang.*;
import java.util.*;
 
class DiamondPattern{
      static public int ReadInteger()
      {
            try
            {
                  String inpString = "";
                  InputStreamReader input = new InputStreamReader(System.in);
                  BufferedReader reader = new BufferedReader(input);
                  String s = reader.readLine();
                  return Integer.parseInt(s);
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
            return -1;
      }
 
      public static void main(String[] args)
      {
            System.out.println("Enter the number till you want a star (*) diamond pattern.");
            
            int n = ReadInteger();
           
            System.out.println("\nHere is the Diamond of Stars\n");
 
            for (int i = 1; i <= n; i++)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print(" ");
                  for (int j = 1; j <= i; j++)
                        System.out.print("*");
                  for (int k = 1; k < i; k++)
                        System.out.print("*");
                  System.out.println();
            }
 
            for (int i = n - 1; i >= 1; i--)
            {
                  for (int j = 0; j < (n - i); j++)
                        System.out.print(" ");
                  for (int j = 1; j <= i; j++)
                        System.out.print("*");
                  for (int k = 1; k < i; k++)
                        System.out.print("*");
                  System.out.println();
            }
 
            System.out.println();
      }
}

Output:


No comments:

Post a Comment