10 Stunning Patterns in C++ You Should Try!
Introduction
Programming is not just about solving problems; it’s also about exploring creativity. Patterns in programming help improve logic-building skills, enhance problem-solving abilities, and make learning enjoyable. This week, I explored 10 exciting patterns using C++ and learned how to implement them step by step. In this blog, I’ll share the journey, along with the code and outputs for each pattern.
Pattern 1: Right-Aligned Triangle with Stars
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (j <= 5 - i)
cout << " ";
else
cout << "*";
}
cout << endl;
}
return 0;
}
Output:
*
**
***
****
*****
Pattern 2: Right-Aligned Triangle with Numbers
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (j <= 5 - i)
cout << " ";
else
cout << i;
}
cout << endl;
}
return 0;
}
Output:
1
22
333
4444
55555
Pattern 3: Incrementing Number Triangle
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
int num = 1;
for (int j = 1; j <= 5; j++) {
if (j <= 5 - i)
cout << " ";
else
cout << num++;
}
cout << endl;
}
return 0;
}
Output:
1
12
123
1234
12345
Pattern 4: Alphabet Pyramid
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
char ch = 'A';
for (int j = 1; j <= 5; j++) {
if (j <= 5 - i)
cout << " ";
else
cout << ch++;
}
cout << endl;
}
return 0;
}
Output:
A
AB
ABC
ABCD
ABCDE
Pattern 5: Pyramid of Stars
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++)
cout << " ";
for (int j = 1; j <= 2 * i - 1; j++)
cout << "*";
cout << endl;
}
return 0;
}
Output:
*
***
*****
*******
*********
Pattern 6: Inverted Pyramid of Stars
Code:
#include <iostream>
using namespace std;
int main() {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++)
cout << " ";
for (int j = 1; j <= 2 * i - 1; j++)
cout << "*";
cout << endl;
}
return 0;
}
Output:
*********
*******
*****
***
*
What I Learned
Logic Building: Patterns helped me strengthen my understanding of nested loops and conditional logic.
Creativity: Experimenting with spaces, characters, and numbers improved my problem-solving skills.
Debugging: Finding and fixing errors in patterns honed my debugging abilities.
Resources Used
YouTube tutorials for beginners.
My curiosity to experiment and create variations.
What’s Next?
In the coming weeks, I’ll explore more advanced patterns, including:
Pascal’s Triangle
Zig-Zag Patterns
Numeric Diamonds
Stay tuned as I continue my journey to mastering patterns in programming!
Conclusion
Patterns are a fun and creative way to practice programming. If you’re new to coding, I highly recommend starting with simple patterns and gradually increasing the complexity. Let me know which pattern you liked the most or suggest one for me to try!
Feel free to check out all my pattern codes on my GitHub Repository https://github.com/Ashishkr99 . Let’s connect and learn together!