Alphabet diamond pattern using For Loop in C++


This C++ code prints a diamond pattern with letters. It takes an input limit n and prints the pattern using nested for loops. The first for loop iterates from i=1 to n and prints the top half of the diamond. The second for loop prints spaces for the left side of the diamond, and the third and fourth for loops print the letters in each row. After printing the top half, the second for loop iterates from i=1 to n-1 and prints the bottom half of the diamond. The third for loop prints spaces for the left side of the diamond, and the fourth and fifth for loops print the letters in each row.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cout<<"\n Enter the limit:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n-i;j++)
        {
            cout<<" ";
        }
        for(j=1;j<i;j++)
        {
            cout<<char(j+64);
        }
        for(j=i;j>=1;j--)
        {
            cout<<char(j+64);
        }
        cout<<"\n";
    }
    for(i=1;i<n;i++)
    {
        for(j=1;j<=i;j++)
        {
            cout<<" ";
        }
        for(j=1;j<n-i;j++)
        {
            cout<<char(j+64);
        }
        for(j=n-i;j>=1;j--)
        {
            cout<<char(j+64);
        }
        cout<<"\n";
    }
    return 0;
}
To download raw file Click Here

Output

         A
        ABA
       ABCBA
      ABCDCBA
     ABCDEDCBA
    ABCDEFEDCBA
   ABCDEFGFEDCBA
  ABCDEFGHGFEDCBA
 ABCDEFGHIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
 ABCDEFGHIHGFEDCBA
  ABCDEFGHGFEDCBA
   ABCDEFGFEDCBA
    ABCDEFEDCBA
     ABCDEDCBA
      ABCDCBA
       ABCBA
        ABA
         A


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples