In this article, we will discuss the Java code to display pyramid star pattern program – using loops
In this post, we will learn how to create Pyramid star pattern using for, while and do-wile loop in Java language
Program 1
Code to display Pyramid using for loop
This program allows the user to enter the number of rows and the symbol then the program displays a full pyramid star pattern with the given symbol using for loop in java language
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class Full_Pyramidfor{
public static voidmain(String args[]){
int rows;
Scanner scan=newScanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer for loop
for(int i=1; i<=rows; i++){//iterates trough rows in pyramid
for(int j=i; j<rows; j++){//innr for loop 1
System.out.print(" ");//print space
}
for(int k=1; k<2*i; k++){//inner for loop 2
if(i==rows ||(k==1|| k == 2*i-1)){
System.out.print(ch);//print input charactor when if condition returns frue
import java.util.Scanner;
public class Full_Pyramidfor{
public static void main(String args[]){
int rows;
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer for loop
for(int i=1; i<=rows; i++){//iterates trough rows in pyramid
for(int j=i; j<rows; j++){//innr for loop 1
System.out.print(" ");//print space
}
for(int k=1; k<2*i; k++){//inner for loop 2
if(i==rows || (k==1 || k == 2*i-1)){
System.out.print(ch);//print input charactor when if condition returns frue
}
else{
System.out.print(ch);//otherwise print input charactor
}
}
System.out.println();//move to next line
}
}
}
import java.util.Scanner;
public class Full_Pyramidfor{
public static void main(String args[]){
int rows;
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer for loop
for(int i=1; i<=rows; i++){//iterates trough rows in pyramid
for(int j=i; j<rows; j++){//innr for loop 1
System.out.print(" ");//print space
}
for(int k=1; k<2*i; k++){//inner for loop 2
if(i==rows || (k==1 || k == 2*i-1)){
System.out.print(ch);//print input charactor when if condition returns frue
}
else{
System.out.print(ch);//otherwise print input charactor
}
}
System.out.println();//move to next line
}
}
}
When the above code is executed it produces the following result
Pyramid pattern using for
Code to display Pyramid using the while loop
Program 2
This program allows the user to enter the number of rows and the symbol then the program displays a full pyramid star pattern with the given symbol using while loop in Java language
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class Full_Pyramidwhile{
public static voidmain(String args[]){
int rows;
Scanner scan=newScanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer while loop
int i=1;
while(i<=rows){//To iterates trough rows in pyramid
int j=i;
while(j<rows){//innr while loop 1
System.out.print(" ");//print space
j++;
}
int k=1;
while( k<2*i){//inner while loop 2
if(i==rows ||(k==1|| k == 2*i-1)){
System.out.print(ch);
}//print given charactor when if condition returns frue
else{
System.out.print(ch);//otherwise print given charactor
}
k++;
}
System.out.println();//move to next line
i++;
}
}
}
import java.util.Scanner;
public class Full_Pyramidwhile{
public static void main(String args[]){
int rows;
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer while loop
int i=1;
while(i<=rows){//To iterates trough rows in pyramid
int j=i;
while(j<rows){//innr while loop 1
System.out.print(" ");//print space
j++;
}
int k=1;
while( k<2*i){//inner while loop 2
if(i==rows || (k==1 || k == 2*i-1)){
System.out.print(ch);
}//print given charactor when if condition returns frue
else{
System.out.print(ch);//otherwise print given charactor
}
k++;
}
System.out.println();//move to next line
i++;
}
}
}
import java.util.Scanner;
public class Full_Pyramidwhile{
public static void main(String args[]){
int rows;
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer while loop
int i=1;
while(i<=rows){//To iterates trough rows in pyramid
int j=i;
while(j<rows){//innr while loop 1
System.out.print(" ");//print space
j++;
}
int k=1;
while( k<2*i){//inner while loop 2
if(i==rows || (k==1 || k == 2*i-1)){
System.out.print(ch);
}//print given charactor when if condition returns frue
else{
System.out.print(ch);//otherwise print given charactor
}
k++;
}
System.out.println();//move to next line
i++;
}
}
}
When the above code is executed it produces the following result
Code to display Pyramid using the do-while loop
Program 3
This program allows the user to enter the number of rows and the symbol then the program displays a full pyramid star pattern with the given symbol using the do-while loop in Java language
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class Full_PyramidDowhile1{
public static voidmain(String args[]){
int rows;
Scanner scan=newScanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer do-while loop
int i=1;
do{//To iterates trough rows in pyramid
int j=i;
do{//innr do-while loop 1
System.out.print(" ");//print space
j++;
}while(j<=rows);//while condition 1
int k=1;
do{//inner do-while loop 2
if(i==rows ||(k==1|| k == 2*i-1)){
System.out.print(ch);
}//print given charactor when if condition returns true
else{
System.out.print(ch);//otherwise print given charactor
}
k++;
}while( k<2*i);//while condition 2
System.out.println();//move to next line
i++;
}while(i<=rows);//while condition for outer loop
}
}
import java.util.Scanner;
public class Full_PyramidDowhile1{
public static void main(String args[]){
int rows;
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer do-while loop
int i=1;
do{//To iterates trough rows in pyramid
int j=i;
do{//innr do-while loop 1
System.out.print(" ");//print space
j++;
}while(j<=rows);//while condition 1
int k=1;
do{//inner do-while loop 2
if(i==rows || (k==1 || k == 2*i-1)){
System.out.print(ch);
}//print given charactor when if condition returns true
else{
System.out.print(ch);//otherwise print given charactor
}
k++;
}while( k<2*i);//while condition 2
System.out.println();//move to next line
i++;
}while(i<=rows);//while condition for outer loop
}
}
import java.util.Scanner;
public class Full_PyramidDowhile1{
public static void main(String args[]){
int rows;
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of rows");
rows=scan.nextInt();
System.out.println("Enter Sympol for use");
char ch=scan.next().charAt(0);
//outer do-while loop
int i=1;
do{//To iterates trough rows in pyramid
int j=i;
do{//innr do-while loop 1
System.out.print(" ");//print space
j++;
}while(j<=rows);//while condition 1
int k=1;
do{//inner do-while loop 2
if(i==rows || (k==1 || k == 2*i-1)){
System.out.print(ch);
}//print given charactor when if condition returns true
else{
System.out.print(ch);//otherwise print given charactor
}
k++;
}while( k<2*i);//while condition 2
System.out.println();//move to next line
i++;
}while(i<=rows);//while condition for outer loop
}
}
When the above code is executed it produces the following result
Program to print Hollow right triangle star pattern in Java In this article, we will discuss the Program to print hollow right triangle star pattern in Java We can display many types of number, star, Alphabet, Binary patterns using for.while and do-while loop in Java language In this post, we…
Rhombus star pattern program in Java|Java patterns In this article, we will discuss the Rhombus star pattern program in Java|Java patterns We can display many types of number, star, Alphabet, Binary patterns using for.while and do-while loop in Java language In this post, we are going to learn how to…
Diamond pattern program in Java language In this article, we will discuss the Diamond star pattern program in Java language we can create star number, alphabet, binary patterns using loops (for, while and do-while loop) in Java language In this post, we are going to learn how to create a diamond…