Alphabet Z shape pattern in java

Making Z pattern


Steps to do this:-
1. take 3 integers i,j,n (i and j is for loop and n is for user input length)

2. take the length from user

3.start the i loop (i=1;i<=n;i++) and start the j loop inside i loop 
(j=1;j<=n;j++) now check this inside j loop that
if(i==1||  i==n|| i+j==n+1)
{
print "*";
}
else

{
print " ";
}
close the j loop and print a new line inside i loop..
i is for printing rows and j is for printing coloumn

Explanation:-
when we check if (i==1) it will print the first row 
***********

when we check if (i==1) it will print the last row




***********

when we check if (i+j==n+1) it will print the right to left line 
                     *
                   *
                *
             *
          *
 and it will print z

4. save ad run

The Source Code

import java.util.Scanner;

 class zpatt
 {
     public static void main(String[] args) {
         Scanner s=new Scanner(System.in);
         int i,j,n;
         System.out.println("enter the length");
         n=s.nextInt();
         for(i=1;i<=n;i++)
         {
             for(j=1;j<=n;j++)
             {
                 if(i==1||i==n||i+j==n+1)
                 {
                     System.out.print("*");
                 }
                 else
                 {
                     System.out.print(" ");
                 }
             }
             System.out.println();
         }
     }
}

OUTPUT


                                    Watch My Video




 

Comments

Popular Posts