How to make V shape pattern in java

Making  V shape pattern




Steps to do this:-

1. take 3 integers i,j,n (i and j is for loop and n is for length)

2. Scan the value from user

3. take int p1=1 and p2=n*2-1 

4.start the i loop (i=1;i<=n;i++) and start the j loop under i section start the loop from (j=1;j<=n*2;j++)
  
if user input 5 then row will be 5 but coloumn will be (n*2=5*2=10)

note* i is for row and j is for coloumn

inside j loop check if (j==p1||j==p2) and print "*" else print" "(a blank string ) close the j loop and do 
p1++;
p2--;
and print a new line
see this :-




5.save and run


The Source Code

import java.util.Scanner;
class v
{
    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 length of the pattern
        System.out.println("enter the length");
        n=s.nextInt();

        int p1=1;
        int p2=n*2-1;

        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n*2;j++)
            {
                if(j==p1||j==p2)
                {
                    System.out.print("*");
                }
                else

                {
                    System.out.print(" ");
                }
            }
            p1++;
            p2--;
            System.out.println();
        }
    }
}



OUTPUT




Watch My Video

Comments

Popular Posts