Java program to display star Pyramid pattern
- Home
- pyramid triangle
- Java program to display star Pyramid pattern
- On
- By
- 0 Comment
- Categories: pyramid triangle, star pattern
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
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
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
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
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
Do-while loop in Java language
Nested for loop in Java language
Nested for loop in C++ language
Nested for loop in Python language