Java Code To Create Pyramid and Pattern

Java Code To Create Pyramid and Pattern

Pyramid and Patterns

In this Article, we will be writing different programs to print different patterns for pyramid which includes half left pyramid, half right pyramid, full pyramid and different types of inverted pyramid in Java.

Program to Create Half Left Pyramid:

        *
      * *
    * * *
  * * * *
* * * * *
Run
import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int spaces = n-1;
        
        for(int i = 0;i < n; i++){
            for(int j = 0; j < spaces; j++){
                System.out.print(" ");
            }
            for(int j = 0; j <= i; j++){
                System.out.print("*");
            }
            System.out.println();
            spaces--;
        }
    }
}

Program to Create Half Right Pyramid:

*
* * 
* * *
* * * *
* * * * *
Run
import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        
        for(int i = 0;i < n; i++){
            for(int j = 0; j <= i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Program to Create Full Pyramid:

        *
      * * *
    * * * * *
  * * * * * * * 
* * * * * * * * *
Run
import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int space = n/2;
        int star = 1;
        
        for(int i = 0;i <= n/2; i++){
            for(int j = 0; j < space; j++){
                System.out.print(" ");
            }
            for(int j = 0; j < star; j++){
                System.out.print("*");
            }
            space--;
            star+=2;
            System.out.println();
        }
    }
}

Program to Create Left Half Down Pyramid:

* * * * *
  * * * *
    * * *
      * *
        *
Run
import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int space = 0;
        int star = n;
        for(int i = 0;i < n; i++){
            for(int j = 0; j < space; j++){
                System.out.print(" ");
            }
            for(int j = 0; j < star; j++){
                System.out.print("*");
            }
            space++;
            star--;
            System.out.println();
        }
    }
}

Program to Create Right Half Down Pyramid:

* * * * *
* * * *
* * *
* *
*
Run
import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int star = n;
        
        for(int i = 0;i < n; i++){
            for(int j = 0; j < star; j++){
                System.out.print("*");
            }
            star--;
            System.out.println();
        }
    }
}

Program to Create Full Down Pyramid:

* * * * * * * * *
  * * * * * * *
    * * * * * 
      * * *
        *
Run
import java.util.*;

public class Main{
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int star = n;
        int space = 0;
        
        for(int i = 0;i <= n/2; i++){
            for(int j = 0; j < space; j++){
                System.out.print(" ");
            }
            for(int j = 0; j < star; j++){
                System.out.print("*");
            }
            star-=2;
            space++;
            System.out.println();
        }
    }
}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription