The pyramid pattern is one of the important topics for each programming language. In this article, we will cover seven important and basic Pyramid Pattern Programs in the JAVA programming language.
The image of a pyramid that we will cover in this article is :
For each Pyramid Pattern in any programming language, you must know the following things :
- Nested loop: Check Details
- Number of rows (number of rows tells us how long you need the pyramid).
The code for these Pyramid Patterns using JAVA is given below. Let’s start one by one to cover all :
Pyramid Pattern using for loop in Java :
import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
public static void main(String args[]) {
try{
int n,i,j;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows");
n= s.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}
Output :
Enter the number of rows
5
*
**
***
****
*****
Check out My Latest post on Developer Helps for some Interesting Insights
↠ What is the Cannot find symbol Error in Java?
↠ How to Find the Size or Length of an Array in JAVA?
↠ How to Copy an Array in JAVA?
↠ How to Move All Zeroes Present in an Array to the End in Java?
↠ Different Examples for First Come First Serve Scheduling in Java
↠ How long does it take to learn Python if you know Java?
↠ How to Initialize an Array in Java?
↠ How to format an instant to a String in Java
Pyramid Pattern using for loop in Java :
import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
public static void main(String args[]) {
try{
int n,i,j;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows");
n = s.nextInt();
for(i=n;i>=1;i--){
for(j=1;j<=i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}
Output :
Enter the number of rows
5
*****
****
***
**
*
Java Program for below Pyramid Pattern :
import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
public static void main(String args[]) {
try{
int n,i,j,space;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows");
n = s.nextInt();
for(i=1;i<=n;i++){
for(space=1;space<=n-i;space++){
System.out.print(" ");
}
for(j=1;j<=i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}
Output :
Enter the number of rows
5
*
**
***
****
*****
Pyramid Pattern Program by using for loop :
import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
public static void main(String args[]) {
try{
int n,i,j,space;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows");
n = s.nextInt();
for(i=n;i>=1;i--){
for(space=1;space<=n-i;space++){
System.out.print(" ");
}
for(j=1;j<=i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}
Output :
Enter the number of rows
5
*****
****
***
**
*
Pyramid Pattern in Java :
import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
public static void main(String args[]) {
try{
int n,i,j,space;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows");
n = s.nextInt();
for(i=1;i<=n;i++){
for(space=1;space<=n-i;space++){
System.out.print(" ");
}
for(j=1;j<2*i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}
Output:
Enter the number of rows
5
*
***
*****
*******
*********
Java Program for the below Pattern :
import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
public static void main(String args[]) {
try{
int n,i,j,space;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows");
n = s.nextInt();
for(i=n;i>=1;i--){
for(space=1;space<=n-i;space++){
System.out.print(" ");
}
for(j=1;j<2*i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}
Output :
Enter the number of rows
5
*********
*******
*****
***
*
Java Program for the below Pattern :
Diamond-Shaped Pyramid is one of the easiest pyramids. This type of Pyramid is made by adding Pyramid Patterns numbers 5 and 6.
import java.util.Scanner;
import java.util.NoSuchElementException;
public class main{
public static void main(String args[]) {
try{
int n,i,j,space,x;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows");
n = s.nextInt();
x=n/2;
for(i=1;i<=x;i++){
for(space=1;space<=x-i;space++){
System.out.print(" ");
}
for(j=1;j<2*i;j++){
System.out.print("*");
}
System.out.print("\n");
}
for(i=x;i>=1;i--){
for(space=1;space<=x-i;space++){
System.out.print(" ");
}
for(j=1;j<2*i;j++){
System.out.print("*");
}
System.out.print("\n");
}
}
catch (NoSuchElementException e) {
System.out.println("Type something in the Stdin box above.... and then Execute");
}
}
}
Output :
Enter the number of rows
6
*
***
*****
*****
***
*