Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! π πΉLearn advanced coding techniques in C& C++ πΉMaster Linux internals & shell scripting πΉDeep dive into Data Structures & Algorithms πΉExplore Embedded Systems & Microcontrollers (8051,UART, RTOS) πΉGet hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
C++ Triangle Pattern Programs 2: Star, Number, Alphabet & Binary Patterns with Output
π’ C++ Program to Print Continuous Number Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num,a=0;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<a++<<" ";
}
cout<<endl;
}
}
Enter the number:
5
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
⭐ C++ Program to Print Star Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;
}
}
Enter the number:
5
*
* *
* * *
* * * *
* * * * *
π€ C++ Program to Print Repeated Alphabet Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<char(i+64)<<" ";
}
cout<<endl;
}
}
Enter the number:
5
A
B B
C C C
D D D D
E E E E E
π€ C++ Program to Print Alphabet Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<char(j+64)<<" ";
}
cout<<endl;
}
}
Enter the number:
5
A
A B
A B C
A B C D
A B C D E
π’ C++ Program to Print Binary Number Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<j%2<<" ";
}
cout<<endl;
}
}
Enter the number:
5
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
π’ C++ Program to Print Repeated Binary Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<i%2<<" ";
}
cout<<endl;
}
}
Enter the number:
5
1
0 0
1 1 1
0 0 0 0
1 1 1 1 1
π’ C++ Program to Print Repeated Number Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<i<<" ";
}
cout<<endl;
}
}
Enter the number:
5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
π’ C++ Program to Print Number Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the number:"<<endl;
cin>>num;
for(int i=1;i<=num;i++)
{
for(int k=num;k>=i;k--)
{
cout<<" ";
}
for(int j=1;j<=i;j++)
{
cout<<j<<" ";
}
cout<<endl;
}
}
Enter the number:
5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
⭐ C++ Program to Print Palindromic Alphabet Triangle Pattern
#include<iostream>
using namespace std;
int main()
{
int num;
cout << "Enter the number:" << endl;
cin >> num;
for(int i = 1; i <= num; i++)
{
// Print spaces
for(int k = num; k >= i; k--)
{
cout << " ";
}
// Print increasing alphabets
for(int j = 1; j <= i; j++)
{
cout << char(j + 64) << " ";
}
// Print decreasing alphabets
for(int j = i - 1; j >= 1; j--)
{
cout << char(j + 64) << " ";
}
cout << endl;
}
return 0;
}
Enter the number:
5
A
A B A
A B C B A
A B C D C B A
A B C D E D C B A
π Explanation
This C++ program prints a palindromic alphabet triangle pattern using nested for loops. The outer loop controls the number of rows, while the first inner loop prints spaces to create the required alignment.
The second inner loop prints alphabets in increasing order from A to the current character. The third inner loop prints the alphabets in decreasing order, creating a mirror image of the first half. Therefore, each row forms a palindrome such as A B C B A.
The expression char(j + 64) converts an integer value into its corresponding uppercase alphabet using ASCII values. For example, 1 + 64 = 65, which represents A, while 2 + 64 = 66, which represents B.
π§ Algorithm
- Start the program.
- Read the number of rows from the user.
- Run an outer loop from
i = 1tonum. - Print spaces before the alphabets to create the required alignment.
- Print alphabets from
Ato the current character using an increasing loop. - Print the alphabets in reverse order using a decreasing loop.
- Move to the next line after completing each row.
- Repeat until all rows are printed.
- Stop the program.
π‘ Key Concept
The pattern is created using two alphabet loops. The first loop prints characters in increasing order, while the second loop prints them in decreasing order.
Increasing: A B C
Decreasing: B A
Final Row: A B C B A
In modern C++, the alphabet conversion can also be written more clearly as:
cout << char('A' + j - 1) << " ";
π C to C++ Conversion
The main changes when converting this pattern program from C to C++ are:
C C++
#include <stdio.h> #include <iostream>
printf() cout
scanf() cin
%c char()
return 0; return 0;
The important character conversion changes from:
printf("%c ", j + 64);
⬇️
cout << char(j + 64) << " ";
⏱ Time Complexity
O(n²) — The program uses nested loops to print spaces and characters for every row. The total number of operations grows approximately quadratically with the number of rows.
πΎ Space Complexity
O(1) — The program uses only a fixed number of variables and does not require additional memory proportional to the input size.
π Search Description
Learn how to print a palindromic alphabet triangle pattern in C++ using nested loops. This beginner-friendly C++ program prints alphabets in increasing and decreasing order to create a symmetric character pattern. It also explains ASCII character conversion and the difference between C and C++ input/output syntax.
π Keywords
palindromic alphabet triangle pattern in C++, C++ alphabet palindrome pattern, alphabet triangle pattern in C++, C++ palindrome pattern program, character palindrome pattern, alphabet pattern using nested loops, C++ pattern programs, ASCII alphabet pattern in C++, C to C++ conversion, nested loops in C++, C++ programming examples
π· Hashtags
#CppProgramming #PatternPrograms #AlphabetPattern #PalindromePattern #CharacterPattern #TrianglePattern #NestedLoops #ASCII #CPlusPlus #Coding #Programming #DSA #1printf
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment