Let's learn java programming language with easy steps

Sunday, 19 November 2017

Alphabet Pattern Programs in Java

Java Alphabet Pattern Programs

Java Alphabet Pattern Programs

In the last article, We saw some most useful and different-different pattern programs in java like number pattern programs in java, star pattern programs in java but here we are going to discuss some basic and useful alphabet pattern programs in java with simple examples one by one.

Here we are creating alphabet pattern programs in java which is mostly asked for fresher and experienced in core java interviews.

Let 's start with alphabet pattern in java.

Alphabet Pattern 1 :

This example will show you characters from a to z in java.

class AtoZ
{
public static void main(String args[])
{
for(char ch = 'a'; ch <= 'z'; ch++)
{
System.out.println(ch);
}
}
}

Output: a
             b
             c
             d
             e
             f
             .
             .
             z

The above example will print characters from a to z.


Alphabet Pattern 2 :

A
AB
ABCe
ABCD
ABCDE

Solution :

class Demo1
{
public static void main(String args[])
{
for(int i = 1; i <6; i++)//for printing 5 rows
{
int alphabet = 65;//creating local variable with ASCII value of A i.e 65
for(int j = 1; j <= i; j++)
{
System.out.print((char)alphabet);//casted int value to char to get alphabet
alphabet++;
}
System.out.println();
}
}
}

Alphabet Pattern 3 :

A
BC
DEF
GHIJ
KLMNO
PQRSTU

Solution :

class Demo2
{
public static void main(String args[])
{
int alphabet = 65;//ASCII value of 'A'
for(int i = 1; i <7; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print((char)alphabet);
alphabet++;
}
System.out.println();
}
}
}

Alphabet Pattern 4 :

A
BB
CCC
DDDD
EEEEE

Solution :

class Demo4
{
public static void main(String args[])
{
int alphabet = 65;
for(int i = 1; i < 6; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print((char)alphabet);
}
System.out.println();
alphabet++;
}
}
}

Alphabet pattern programs can be created by using for loop, while loop.

Alphabet Pattern 5 :

A
BA
CBA
DCBA

Solution :

class Demo4
{
public static void main(String args[])
{
for(int i = 0; i <5; i++)
{
for(int j = 0, k = i; j <= i; j++, --k)
System.out.print((char)('A' + k));
System.out.print("\n");
}
}
}

Alphabet Pattern 6 :

                              A
                            ABA
                          ABCBA
                        ABCDCBA
                      ABCDEDCBA

Solution :

class Demo5
{
public static void main(String args[])
{
char c;
for(int i = 1; i <= 5; ++i)
{
c = 'A';
for(int j = i; j <5; ++j)
{
System.out.print(" ");

}
for(int k = 1; k <=i; ++k)
{
System.out.print(c);
++c;
}
c -= 2;
for(int l = 1; l <i; ++l)
{
System.out.print(c);
--c;
}
System.out.println();
}
}
}

Visit : 

Java Array Programming Interview Questions.
Java String Programming Interview Questions.

Here we discussed Java alphabet pattern programs which is quite useful in java interviews. We will see some other most important alphabet pattern programs in coming articles.

Share:

2 comments:

  1. Your all java pattern post very useful. thanks for sharing this.

    ReplyDelete
  2. how can we write:
    a b c d e
    b c d e
    c d e
    d e
    e

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate