How to make Cross make pattern in java

Making cross pattern



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

2.take a value from user for length

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

note* i==j will print the left lines and i+j==n+1 will print the right lines

4. close the j loop and print an new line 

5.Save and run


The Source Code

import java.util.Scanner;
class cross
{
    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==j||i+j==n+1)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}


OUTPUT



Watch My Video 

Comments

Popular Posts