pyramid triangle

Java program to display star Pyramid pattern

Java program to display star Pyramid pattern

In this tutorial, we will discuss about Java program to display star Pyramid pattern.

Here, we can see four type of pyramid pattern displays using nested for loop in Java using stars

Code to star Pyramid pattern 1

Star pyramid pattern

Program

import java.util.Scanner; 
public class Pyramid_pattern1{
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //Scanner class in java
System.out.print("Enter the rows you want");
int rows=sc.nextInt();
System.out.println("");

for (int i=1; i<=rows; i++){//outer forloop
  for (int j=1; j<=(rows-i)*2; j++){
  System.out.print(" ");//create space for pyramid shape
  }
for (int k=i; k>=1; k--){//inner for loops
  System.out.print(" "+"*");//create left half            
}          
for (int l=2; l<=i; l++){
  System.out.print(" "+"*");    //create right half
}//end outer for loop                            
System.out.println();
}
                                                   
}

When the above code is executed, it produces the following results:

Enter the rows you want
                                               *
                                           *  *  *
                                       *  *  *  *  *
                                   *  *  *  * *  *  *
                               *  *  *  *  *  *  *  *  *
                            *  *  *  *  * *  *  *  *  *  *
                        *  *  *  *  *  *  *  *  *  *  *  *  *
                    *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
                *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *
                               

 

Code to  star Pyramid pattern 1

Inverted star Pyramid

Program

import java.util.Scanner;
class invertedPyramid1{
public static void main(String args[]){
Scanner sc=new Scanner(System.in); //Scanner class in java
System.out.print("Enter the rows you want");
int rows=sc.nextInt();
System.out.println(" ");
int i,j;
for(i=rows; i>=1; i--){//outer for loop(parent)
    for(j=i; j<=rows; j++){ //inner for loop 1(child)
       System.out.print(" "); //print initial space for piramid
            }
    for(j=1; j<=(2*i-1); j++){ //inner for loop 2(child)
       System.out.print("*"); 
            }
            System.out.println(" ");//print next line

        }
    }
}

When the above code is executed, it produces the following results:

 

*****************
  ***************
    *************
      ***********
        *********
          *******
            *****
              ***
                *

Code to star Pyramid pattern 3

Right leaned pyramid triangle

Program 3

import java.util.*;
class PyramidTriangle3{
public static void main(String args[]){
int i,j,row;
Scanner s=new Scanner(System.in);
System.out.println("Enter value for row: ");
row=s.nextInt();
for(i=1; i<=row; i++){
   for(j=1; j<=i; j++){
System.out.print("*");
}
System.out.println(" ");
}
for(i=row; i>=1; i--){
   for(j=1; j<i; j++){
System.out.print("*");
}
System.out.println(" ");
}
}
}

When the above code is executed, it produces the following results:

Enter value for row:
8
*
**
***
****
*****
******
*******
********
*******
******
*****
****
***
**
*

Code to star Pyramid pattern 4

Left leaned pyramid triangle

 

import java.util.*;
class PyramidTriangle4{
public static void main(String args[]){
int i,j,row;
Scanner s=new Scanner(System.in);
System.out.println("Enter value for row: ");
row=s.nextInt();
 for(i=1; i<=row; i++){
   for(j=i; j<row; j++){
       System.out.print(" ");
   }
   for(j=1; j<=i; j++){
   System.out.print("*");
     }
System.out.println("");
}

for(i=row; i>=1; i--){
   for(j=i; j<=row; j++){
       System.out.print(" ");
   }
   for(j=1; j<i; j++){
   System.out.print("*");
     }
System.out.println("");
}
}
}

When the above code is executed, it produces the following results:

              *
            **
          ***
        ****
      *****
    ******
  *******
********
  *******
    ******
      *****
        ****
          ***
            **
              *

 

Suggested for you

Java program to print star pyramid pattern 

C program to print star pyramid pattern 

C++ program to print star pyramid pattern 

Python program to print star pyramid pattern 

 

For loop in C++ language

For loop in Java language

For loop in C language

For loop in Python language

 

Nested for loop in Java language

Nested for loop in C++ language

Nested for loop in C language

Nested for loop in Python language

C program to pyramid number pattern
pyramid number pattern in Java using for loop
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago