Pyramid Pattern Programs in JAVA

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.
The image of a pyramid that we will cover in this article is :

Pyramid Pattern Programs in JAVA

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.*;
class HelloWorld {
public static void main(String[] args) {
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");
}
}
}

Output :

Enter the number of rows
5
*
**
***
****
*****

Pyramid Pattern using for loop in Java :

Pyramid Pattern Programs in JAVA
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
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");
}
}
}

Output :

Enter the number of rows
5
*****
****
***
**
*

Java Program for below Pyramid Pattern :

Pyramid Pattern Programs in JAVA
import java.util.*;
public class Main
{
public static void main(String[] args) {
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");
}
}
}

Output :

Enter the number of rows

5
         *
        **
       ***
      ****
     *****

Pyramid Pattern Program by using for loop :

import java.util.*;
class HelloWorld 
{
public static void main(String[] args) {
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");
}
}
}

Output :

Enter the number of rows
5
*****
 ****
  ***
   **
    *

Pyramid Pattern in Java :

import java.util.*;
class Helloworld
{
public static void main(String[] args) {
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");
}
}
}

Output:

Enter the number of rows
5
         *
        ***
       *****
      *******
     *********

Java Program for the below Pattern :

import java.util.*;
class Main
{
public static void main(String[] args) {
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");
}
}
}

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.*;
class Main
{
public static void main(String[] args) {
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");
}
}
}

Output :


Enter the number of rows
6
  *
 ***
*****
*****
 ***
  *