How to make Pyramid Pattern in java

Making Pyramid Pattern







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

2.now start i loop from (i=1;i<=n;i++) here the loop starts from 1 to user input 

3. now start k loop inside i loop and start it (k=n-1;k>=i;k--) this is tricky step :) here this loop start from (n-1) (ex:- if user input 7 it will start from 6 to i) in this loop we have to print (" ") a blank string it will print the blank space ,

4. now start the j loop inside i block start it from(j=1;j<=i;j++) here we have to print ("* ") a asterisk and a blank a space 

5. now close the j loop and print new line inside i loop

6.save and run

The Source Code

import java.util.Scanner;
class pyr
{
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        int i,j,k,n;// i,j,k is for loop and n is for user input
        System.out.println("enter the length");
        n=s.nextInt();
        for(i=1;i<=n;i++)
        {
            for(k=n-1;k>=i;k--)
            {
                System.out.print(" ");
            }
            for(j=1;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

OUTPUT

  
WATCH MY VIDEO 

 

Comments

Popular Posts