Print alphabet mirror image pattern in C++

In this tutorial we will learn how to print the alphabet mirror image pattern.

The program accepts the integer and prints the pattern as output.

The variable ‘rows’ is obtained and the variable ‘alphabet’ is calculated by,

alphabet=’a’+rows which denotes up to which character the pattern should be printed.

int rows;
cin>>rows;
char alphabet='a'+rows;

Pattern to be printed:

a b c * c b a
a b * * * b a
a * * * * * a


Code to print the left side alphabet pattern:




for(int i=1;i<=rows;i++){
       for(int j='a';j<alphabet;j++){
               cout<<static_cast<char>(j)<<" ";
       }
}

In the above code,

The left side alphabet pattern is printed. The first for loop denotes the number of rows the alphabets to be printed.

The variable alphabet holds the value ‘a’+number. The characters are printed up to that alphabet.

The second for loop the variable j is initialised with ‘a’ (ASCII) value and the for loop is executed until j is less than the variable alphabet.

Therefore the above code prints the left side alphabet pattern,

a b c
a b
a

Similarly the alphabet mirror image pattern is obtained.

for(int i=1;i<=rows;i++){
   for(int j=alphabet-1;j>='a';j--){
               cout<<static_cast<char>(j)<<" ";
       }
alphabet--;
}

static_cast<char>(j) is used to print the character when the ASCII values are given.

The for loop prints the character in the reverse order and the variable alphabet is also decremented.

c b a
  b a
    a

INPUT 1:

Enter the number of rows: 3

CPP code to print the alphabet mirror image pattern:

#include <iostream.h>
using namespace std;
int main()
{
   cout<<"Enter the number of rows:"<<"\t";
   int rows,count=0;
   cin>>rows;
   int temp=1,flag=0;
   char alphabet='a'+rows;
cout<<"The Alphabet Mirror Image Pattern:"<<"\n";
   for(int i=1;i<=rows;i++,flag=0){
//Code for the left side alphabets.
       for(int j='a';j<alphabet;j++){
               cout<<static_cast<char>(j)<<" ";
       }
//Code to print the middle triangle.
      while(flag!=2*i-1){
          cout<<"* ";
          ++flag;
      }
//Code for the right side alphabets.
       for(int j=alphabet-1;j>='a';j--){
               cout<<static_cast<char>(j)<<" ";
       }
       cout<<"\n";
       alphabet--;
       temp++;
   }
}

In the above code,

The first for loop decides the number of rows to be printed.

The next for loop is for printing the left side alphabet pattern.

The variable j holds the value ‘a’ and the condition is checked whether it is less than the alphabet and the respective character is printed.

The while loop is used to print the * pattern in-between the mirror image until the condition becomes flag==2*i-1.

The last for loop is for printing the mirror image pattern of alphabets in the right side.

The variable is initialised with the variable alphabet and it is checked whether it is greater than or equal to ‘a’ and prints the respective character accordingly in the decremented order.

The for loop is executed for given number of rows and prints the pattern as output.

OUTPUT 1:

Alphabet mirror image pattern:
a b c * c b a
a b * * * b a
a * * * * * a


INPUT 2:

Enter the number of rows: 4

OUTPUT 2:

a b c d * d c b a
a b c * * * c b a
a b * * * * * b a
a * * * * * * * a

Thus the program prints the alphabet mirror image output.

We hope this tutorial helped you to understand how to print the alphabet mirror image pattern.

Also, read:


Leave a Reply

Your email address will not be published. Required fields are marked *