# Pyramid Pattern Programs in JAVA

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.

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

## 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
*
***
*****
*****
***
*``````

## Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

### Java Online Quiz

Level up your coding skills with our interactive programming quiz!