Alphabet Pyramid pattern in Java using while loop
- Home
- Alphabet Pattern
- Alphabet Pyramid pattern in Java using while loop
- On
- By
- 2 Comments
- Categories: Alphabet Pattern, pyramid triangle
Alphabet Pyramid pattern in Java using while loop
Alphabet Pyramid pattern in Java using while loop
In this tutorial, we will discuss about Alphabet Pyramid pattern in Java using while loop
Alphabet Pyramid pattern in C using while loop
In this program, we are going to learn about how to display Alphabet pyramid pattern using while loop in Java programming language
Here, we display a Alphabet pyramid pattern program with coding using nested while loop and also we get input from the user using Scanner class in the Java language
The user can provide numbers as they wish and get the Alphabet pyramid pattern according to their input.
Alphabet pyramid pattern 1
Program 1
import java.util.Scanner;//import the scanner class
class PyramidPatternWhile{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);//create a scanner object
System.out.println("Enter the number of rows");
int rows=scan.nextInt();
int i,j;
i=1;
while(i<=rows){
j=1;
while(j<=rows-i){//print space
System.out.print(" ");
j++;
}
j=1;
while(j<=i){//print left site pattern
System.out.print((char)(j+64));
j++;
}
j=i-1;
while(j>=1){//print right site pattern
System.out.print((char)(j+64));
j--;
}
i++;
System.out.println();
}
}
}
When the above code is executed, it produces the following results
Enter the number of rows
6
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
Alphabet pyramid pattern 2
Program 2
import java.util.Scanner;//import the scanner class
class PyramidPatternWhile1{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);//create a scanner object
System.out.println("Enter the number of rows");
int rows=scan.nextInt();
int i,j;
i=1;
while(i<=rows){//outer while loop
j=1;
while(j<=rows-i){//inner while loop 1
System.out.print(" ");//prinet space
j++;
}
j=i;
while(j>0){//inner while loop 2
System.out.print((char)(j+64));//print left site pattern
j--;
}
j=2;
while(j<=i){//print right side pattern
System.out.print((char)(j+64));
j++;
}
i++;
System.out.println();
}
}
}
When the above code is executed, it produces the following results
Enter the number of rows
6
A
BAB
CBABC
DCBABCD
EDCBABCDE
FEDCBABCDEF
Alphabet pyramid pattern 3
Program 3
import java.util.Scanner;//import the scanner class
class PyramidPatternWhile2{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);//create a scanner object
System.out.println("Enter the number of rows");
int rows=scan.nextInt();
int i,j;
i=1;
while(i<=rows){
j=1;
while(j<=rows-i){
System.out.print(" ");
j++;
}
j=1;
while(j<=i){
System.out.print((char)(char)(j+64)+" ");
j++;
}
i++;
System.out.println();
}
}
}
When the above code is executed, it produces the following results
Enter the number of rows
5
A
A B
A B C
A B C D
A B C D E
Alphabet pyramid pattern 4
Program 4
import java.util.Scanner;//import the scanner class
class PyramidPatternWhile3{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);//create a scanner object
System.out.println("Enter the number of rows");
int rows=scan.nextInt();
int i,j;
i=1;
while(i<=rows){
j=1;
while(j<=rows-i){
System.out.print(" ");
j++;
}
j=1;
while(j<=i){
System.out.print((char)(char)(i+64)+" ");
j++;
}
i++;
System.out.println();
}
}
}
When the above code is executed, it produces the following results
Enter the number of rows
6
A
B B
C C C
D D D D
E E E E E
F F F F F F
Alphabet pyramid pattern 5
Program 5
import java.util.Scanner;//import the scanner class
class PyramidPatternWhile4{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);//create a scanner object
System.out.println("Enter the number of rows");
int rows=scan.nextInt();
int i,j;
i=rows;
while(i>=1){
j=1;
while(j<=rows-i){
System.out.print(" ");
j++;
}
j=1;
while(j<=2*i-1){
if(j<=i)
System.out.print((char)(char)(j+64)+" ");
else
System.out.print((char)(char)(2*i-j+64)+" ");
j++;
}
i--;
System.out.println();
}
}
}
When the above code is executed, it produces the following results
Enter the number of rows
5
A B C D E D C B A
A B C D C B A
A B C B A
A B A
A
Suggested for you
Operator in Java language
While loop in Java language
Nested while loop in Java language
2 Comments
mukesh July 28, 2019 at 6:09 am
55555
54444
54333
54322
54321
this type of output how to write the logic taile me any one
Karmehavannan August 14, 2019 at 2:55 am
import java.util.Scanner;
class SquarePattern1{
public static void main(String args[]){
int i,j,k; //variable declaration
Scanner scan=new Scanner(System.in);
//create a scanner object for input
//get input from the user for rows
System.out.print(“Enter the number of rows: “);
int rows=scan.nextInt();
System.out.print(“\n”);
int count=0;
/*Print this pattern
5 5 5 5 5
5 4 4 4 4
5 4 3 3 3
5 4 3 2 2
5 4 3 2 1*/
for(i=rows; i>=1; i–){
for(j=rows; j>=i; j–){
System.out.print(j+” “);
count=j;
}
for(k=rows-i+1; k