For loop pattern programs in C and C++
Practicing pattern programs is the fastest way to master for loops in C and C++. Pattern programs are a key to strengthening logic-building.
While Python may be the most accessible language to write pattern code, It is worthwhile to learn the same in C/C++ as it has less abstraction and provides better tools to practice logic building.
Simple number matrix program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
//Number pyramid
for(int i=1;i<=a;i++) {
for(int j=1;j<=a;j++) {
std::cout<<i<<" ";
}
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
Code language: C++ (cpp)
Consecutive numbers matrix program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
for(int i=1; i<=a*a; i+=a) {
for(int j=i; j<i+a; j++) {
std::cout<<j<<" ";
}
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Code language: C++ (cpp)
Number triangle program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
for(int i=1; i<=a; i++) {
for(int j=i; j>=1; j--) {
std::cout<<j<<" ";
}
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Code language: C++ (cpp)
Number pyramid program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
for(int i=1; i<=a; i++) {
for(int m=1; m<=a-i; m++) {
std::cout<<" ";
}
for(int n=i; n>=2; n--) {
std::cout<<n<<" ";
}
for(int j=1; j<=i; j++) {
std::cout<<j<<" ";
}
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
Code language: C++ (cpp)
Alphabet matrix program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
for(int i=1;i<=a;i++) {
for(int j=(i-1)*a;j<i*a;j++) {
char ch = 'A' + j;
std::cout<<ch<<" ";
}
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
Code language: C++ (cpp)
Alphabet triangle program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
for(int i=a-1; i>=0; i--) {
for(int j=i; j<a; j++) {
char ch = 'A' + j;
std::cout<<ch<<" ";
}
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
E
D E
C D E
B C D E
A B C D E
Code language: C++ (cpp)
Star pyramid program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
for(int i=1; i<=a; i++) {
for(int j=1; j<=a-i; j++) {
std::cout<<" ";
}
for(int k=i; k>=1; k--) {
std::cout<<"* ";
}
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
*
* *
* * *
* * * *
* * * * *
Code language: C++ (cpp)
Mixed pattern program
#include <iostream>
int main() {
int a;
std::cout<<"Enter a number: ";
std::cin>>a;
int i = 1;
while(i<=a) {
int j = 1;
while(j<=a-i+1) {
std::cout<<j<<" ";
j++;
}
int l = (i-1)*2;
while(l>0) {
std::cout<<"* ";
l--;
}
int m = a-i+1;
while(m>=1) {
std::cout<<m<<" ";
m--;
}
i++;
std::cout<<"\n";
}
}
Code language: C++ (cpp)
Output pattern
Enter a number: 5
1 2 3 4 5 5 4 3 2 1
1 2 3 4 * * 4 3 2 1
1 2 3 * * * * 3 2 1
1 2 * * * * * * 2 1
1 * * * * * * * * 1
Code language: C++ (cpp)
Conclusion
It is important to have concise logic building along with syntactic knowledge of a language. Now, you are all set to tackle any pattern problem that you encounter.
Checkout our C++ Programming BootCamp
Sharing is caring
Did you like what Sahil Sagwekar wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: