In this tutorial we will learn how can we Print Alphabet Pattern in Java. This type of program often asked in Job interviews and Examinations. With the help of this program, you will learn about for loop in Java program. We will use 2 for loop to complete this program and increment the alphabet by 1. Thus it will print the half pyramid pattern of alphabets.
Print Alphabet Pattern in Java
class AlphabetsPattern
{
public static void main(String abc[])
{
for(int i=1;i<5;i++)
{
int alphabet=65; //ASCII value of A
for(int j=1;j<=i;j++)
{
System.out.print((char)alphabet);
alphabet++;
}
System.out.println();
}
}
}
Output
A
AB
ABC
ABCD
class AlphabetPattern
{
public static void main(String abc[])
{
int alphabet=65;
for(int i=1;i<5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print((char)alphabet);
}
System.out.println();
alphabet++;
}
}
}
Output
A
BB
CCC
DDDD
class PrintAlphabetPattern
{
public static void main(String abc[])
{
for(int i=1;i<10;i++) // increase count 10 to 27 to print till "z"
{
String strChars="";
int alphabet=97; //ASCII value of A = 65 and a=97
for(int j=1;j<=i;j++)
{
strChars=strChars+(char)alphabet + " + ";
alphabet++;
}
System.out.println("(" + strChars.substring(0,strChars.length()-3) + ")");
}
}
}
Output
(a)
(a + b)
(a + b + c)
(a + b + c + d)
(a + b + c + d + e)
(a + b + c + d + e + f)
(a + b + c + d + e + f + g)
(a + b + c + d + e + f + g + h)
(a + b + c + d + e + f + g + h + i)