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:

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

 

Similar post

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 

Floyd’s triangle number pattern using for loop in C

Floyd’s triangle pattern using nested for loop in Java

Floyd’s triangle pattern using nested while loop in Java

Hollow pyramid triangle pattern in C++ language

Rhombus pattern in Java using for loop

Rhombus pattern in C using while loop

Rhombus pattern in C++ using do-while loop

 

Suggested for you

For loop in Java language

For loop in C++ language

For loop in C language

For loop in Python language

 

While loop in Java language

While loop in C language

While loop in C++ language

While loop in Python language

 

Do-while loop in Java language

Do-while loop in C language

Do-while loop in C++ 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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

2 days ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

5 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago