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--; } } }
Note:
In the above Example, we have initialized a variable named spaces. The outer loop for rows will start from 0 and iterates till the input number n. In the Outer loop, Two other loop are iterating which prints spaces ( )and stars (*) respectively.
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(); } } }
Note:
In the above Example, We have initialized two variables to print spaces and stars. In the outer loop, there are two for loop that are Iterating to print the stars and spaces respectively.
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
Login/Signup to comment