Go to Top

Wednesday 3 August 2011

To print the given pattern up to a user-defined input

Question 32 : Write a program in Java to print the following pattern (shown for 10) : 


         * 
        * * 
       * * * 
      * * * * 
     * * * * * 
    * * * * * * 
   * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 
* * * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
    * * * * * * 
     * * * * * 
      * * * * 
       * * * 
        * * 
         * 

Java Program : 

import java.io.*;
class pattern_20
{
 static void main()throws IOException
 {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     int n,i,j,k;
     System.out.print("Enter the length (in terms of *'s) : ");
     n=Integer.parseInt(br.readLine());
     System.out.print("\n");
     for(i=n-1;i>=0;i--)
     {
         for(k=1;k<=i;k++)
         {
             System.out.print(" ");
            }
         for(j=n;j>=i+1;j--)
         {
             System.out.print("*");
             System.out.print(" ");
            }
            System.out.print("\n");
        }
        for(i=1;i<=n-1;i++)
        {
            for(k=1;k<=i;k++)
            {
                System.out.print(" ");
            }
            for(j=n-1;j>=i;j--)
            {
                System.out.print("*");
                System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
}

1 comment:

ShareThis