Showing posts with label Java codes for alphabet G. Show all posts
Showing posts with label Java codes for alphabet G. Show all posts

Java program for the pattern of 'G' | Patterns in JAVA | JAVA POINT

 

Java Program for G



Ques - Write a java program for the following pattern of 'G' :


* * * *                      

*               

*               

*     * * *  

*     *   * 

*     *   * 

* * * *   *  


PROGRAM :


import java.util.Scanner;

public class PatternG

{

    public static void main(String[] args)

    {

        System.out.println("Enter the number of rows : ");

        Scanner scan=new Scanner(System.in);

        int n=scan.nextInt();

        System.out.println();

        for (int i=0;i<n ;i++ )

        {

            for (int j=0;j<n-1 ;j++ )

            {

                if (j==0 || (i==0 || i==n-1) && j<=n/2 || (j==n/2 || j==n- 

                      2) && i>=n/2 || (i==n/2 && j>=n/2))

                {

                    System.out.print("* ");

                }

                else

                {

                    System.out.print("  ");

                }

            }

            System.out.println();

        }

    }

}


/* Output :

 

Enter the number of rows : 7


* * * *                      

*               

*               

*     * * *  

*     *   * 

*     *   * 

* * * *   * 

 

*/