Alphabet N pattern in java

N 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 a length from user

3. start the i loop from (i=1;i<=n;i++) start the j loop inside i loop and from (j=1;j<=n;j++) now check 
if(j==1 ||j==n ||i==j)
{
print "*"
}
else
{
print " "
}
close the j loop and print a new line inside i loop

Explanation:-
when we check if j==1 then it will print the first column
*
*
*
*
*
when we check if j==n it will print the last column
                    *
                    * 
                    *
                    *
when e check if i==j it will print where i and j's value is same
*
 *
  *
   *
    *
at the end of the loop we will get N
4.save and run
The Source Code
import java.util.Scanner;
class npattern
{
    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==j)
                {
                    System.out.print("*");
                }
                else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

OUTPUT

Watch My Video

Comments

Popular Posts