I pattern in java
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.now take the input from user
3.start the i loop (i=1;i<=n;i++) and start the j loop inside the i loop (j=1;j<=n;j++)
i is for printing row and j is for printing coloumn
3. now check in j loop that if(i== 1|| i==n || j==(n/2)) and print "*"
else print " " (a blank string ) close the j loop and print a new line inside i loop
when,i==1 will check it will print the 1st row
when,i==n will check it will print the last row
when,j==n/2 will check it will print the middle column
4.save and run
The Source Code
import java.util.Scanner;
class i
{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int i,j,n;// i and j is for loop and n is for user input
System.out.println("enter the number");
n=s.nextInt();
for(i=1;i<=n;i++)
{
for (j=1;j<=n ;j++ ) {
if(i==1||i==n||j==(n/2))
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Watch My Video
Comments
Post a Comment
if you like it or have any doubts please let me know