Pyramid pattern in java

In this article we are going to print pyramid pattern using java.

Example :- if n=4.

Pyramid pattern in java

    Steps:-

    • Take input for number of lines in pyramid using scanner.
    • Run a loop for all the lines in pyramid lets call it loop 1.
    • inside loop 1 run a loop for printing space .
    • inside loop 1Run another loop to print *.

    Java code to print pyramid pattern:-

     package gangforcode;
    import java.util.Scanner;
    public class PyramidPattern {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n =sc.nextInt();
    		int i=1;
    		while(i<=n) {
    			int j=1;
    			while(j<=(n-i)) {
    				System.out.print(" ");
    			j++;
    			}
    		int k=1;
    		while(k<=(2*i-1)) {
    		System.out.print("*");
    		k++;
    		}
    		i++;
    		System.out.println();
    		}
    		
    	}
    
    }
    

    Output:-

    Pyramid pattern using java code.

    You can use same logic to print pyramid pattern using another programing languages like c,c++,python etc.

    Leave a Comment