Showing posts with label Java program for 'H'. Show all posts
Showing posts with label Java program for 'H'. Show all posts

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

 

Java Program for H



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


*.    *

*     *

*     *

*****

*     *

*     *

*     *


PROGRAM :


import java.util.Scanner;

class PatternH

 

 {

     public static void main(String []args)

     {

         Scanner sc=new Scanner(System.in);

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

         int i, j, n;

         int r=sc.nextInt();

         int c=r-2;

         if(r%2!=0)

         {

             n=r/2;

         }

         else 

         {

             n=(r/2)-1;

         }    

         for(i=0;i<r;i++)

         {

             for(j=0;j<c;j++)

             {

                 if((j==0)||(j==(c-1)))

                 {

                     System.out.print("*");

                 }     

                 else if(i==n)

                 {

                     System.out.print("*");

                 }    

                 else

                 {

                     System.out.print(" ");

                 }

             }

             System.out.println();

         }

        }

 }


/* Output :

 

Enter the number of rows : 7


*     *

*     *

*     *

*****

*     *

*     *

*     *

 

*/