Alphabet H shape pattern in java
1. take 3 integers i,j,n ( i and j is for loop and n is for user input number )
2. take a length from user
3. start the i loop from (i=1;i<=n;i++) and start the j loop inside the i loop from (j=1;j<=n;j++) now check that
if(j==1 || j==n || i==(n/2))
{
print "*";
}
else
{
print " ";
}
close the j loop and print a new line inside i loop
i loop print the row and j loop print the column
Explanation-
when we check if (j==1) it will print the first column
*
*
*
*
*
*
when we check if (j==n) it will print the last column
*
*
*
*
*
*
when we check if(i==n/2) it will print the middle row
***********
and at the end of the loops it will print H
4.save and run
The Source Code
import java.util.Scanner;
class hpatt
{
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 (j == 1 || j == n || i == (n / 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
OUTPUT
Watch My Video
Comments
Post a Comment
if you like it or have any doubts please let me know