How to solve C++ pattern problems

To develop the skills in any Programming language one has to be clear with their basic concept. Last post I wrote about “How you can improve your programming logics“. There are many peoples who are confused with their C++ patterns, almost any kind of patterns can be made in C++. Learn here how to write a program to print patterns. You just have to take a good look at the programming codes needed. All the patterns in C++ can be made using ‘for’ loop. Let’s see how. I am now going to show you step by step to write code for patterns.

Steps for solving C++ pattern

Step 1: Your first step should be to know what actually the logic you need to write.

Step 2: Write in the paper, what the output you need.

Step 3:
Now start solving the pattern.

Take an example: suppose you have to solve the following simple pattern.

11111
22222
33333
44444
55555

The code should be as follows:

#include <iostream>
using namespace std;
int main(){
for(int i=1; i<=5; i++){
for(int j=1; j<=5; j++){
cout<<i;
}
cout<<endl;
}
}

The above code just represents that the “for” loop runs a loop once for every value i.e. the first “for” loop runs the loop 5 times(1-5) and for every value of “i” second “loop” runs the loop 5 times again. The second “for” loop gives the value of “i” which “cout” 1 for five times then 2 for five times then 3 and so on…

So what I mean that you can solve any type of pattern by just “for” loop only. The above code was just very simple, lets know how to write some tough patterns. See the example below.

1
22
333
4444
55555
The program changes a little like 

for(int i=1; i<=5; i++){
for(int j=1; j<=i; j++){
cout<<i;
}

We just changed the second “for” loop so now when the loop runs the first “for” loop runs five times (1-5) but the second “for” loop runs only till “i” times. so the above loop pattern is obtained.

This was a bit tough but there are tougher patterns. Look below:

    1
22
333
4444
55555

Here we need to print the space for first four place and then 1. In second line we need to decrease the space to three place then we have to print “22” then in third line we have to decrease space to two and print “333” and so on…

So the code should contain the the ‘for’ loop for printing space first then another ‘for’ loop for printing numbers so we totally need 3 “for” loops. The code should be as follows:

#include <iostream>
using namespace std;
int main(){
for(int i=1; i<=5; i++){
for(int j=4; j>=i; j--){
cout<<" ";
}
for(int k=1; k<=i; k++){
cout<<i;}
cout<<endl;
}
}

So if you got confused with the above code here is the explanation. The first “for” loop runs a loop of 5. The second “for” loop also runs a loop but in opposite direction i.e. from 4 to ‘i’, where ‘i’ is firstly 1 then 2 and so on. The third “for” loop only prints the last number i.e. 1 then in second line 22 then 33 and so on…The “cout<<i;” is used at last because ‘i’ prints 1, 22, 333, etc., but ‘j’ and ‘k’ can’t print it row-wise.

The patterns in C++ can be more complicated. So hope you understood somethings out of the above post. 


Related Articles

Leave a Comment