2. Page 1 of 10
Pattern 1
Explanation of the logic
In above figure you see certain number of
rows and columns. You can see in 1st
row we
have 1 star, in 2nd
row we have 2 stars. So in
every row, we have row number of stars.
In pattern printing always parent loop is for
rows and inner loop is for columns.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=5;row++)
{
for(col=1;col<=row;col++) /*printing no of
stars for each row*/
{
cout<<'*';
}
cout<<endl;
}
return 0;
}
Pattern 2
Explanation of the logic
3. Page 2 of 10
In first row we have 5 stars, in second row we
have 4 stars and so on. So no of stars are
decreasing by 1 as you go to the next row.
Logic is same as of previous pattern, only
difference is that we have to decrement the
stars this time.
Program
#include <iostream>
using namespace std;
int main(){
int row,col;
for(row=1;row<=5;row++){
for(col=5;col>=row;col--) //printing no of stars
for each row
{
cout<<'*';
}
cout<<endl;
}
return 0;
}
Pattern 3
Explanation of the logic
In above figure, the symbol ‘-’ represents
spaces. In first row, we need to print 4 spaces
and then 1 star. In second row, we need to
print 3 spaces and 2 stars. So we see that no
of spaces are decreasing and no of stars are
increasing.
If we divide above figure in two parts then we
will have
Part 1: Part 2:
We have already written codes for these two
patterns previously. So we just need to
combine these two to get the results.
4. Page 3 of 10
Program
#include <iostream>
using namespace std;
int main(){
int row,col,col2;
for(row=1;row<=5;row++){
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=row;col2++) //printing no of
stars for each row
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Pattern 4
Explanation of the logic
So first analyze the above figure. You see that
logic is exactly same as above program,
difference is that, when you will print the
stars, then provide a space after that i.e.
instead of printing “*” , you should write “*-”
in cout command. The ‘-’ symbol above
represents space here for ease of
understanding.
Program
#include <iostream>
using namespace std;
int main(){
int row,col,col2;
for(row=1;row<=5;row++){
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=row;col2++)//printing no of
stars for each row
{
cout<<"* "; //provide space after star by
hitting space bar of your keyboard
5. Page 4 of 10
}
cout<<endl;
}
return 0;
}
Pattern 5
Explanation of the logic
Here the no of stars to be printed follow the
fomula: (ROW*2)-1.
For example, consider the no of stars to be
printed in 4th
row.
(4*2)-1 =7.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
for(row=1;row<=5;row++)
{
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=(row*2)-1;col2++)//printing
no of stars for each row
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
6. Page 5 of 10
Pattern 6
Explanation of the logic:
This logic is same as previous logic of pattern
6. What we need is to add its reverse part
below it.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
for(row=1;row<=5;row++){
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=(row*2)-1;col2++)//printing
no of stars for each row
{
cout<<"*";
}
cout<<endl;
}
for(row=1;row<=4;row++){
for(col=1;col<=row;col++)
{
cout<<" ";
}
for(col2=7;col2>=(row*2)-1;col2--)
{
cout<<"*";
}
cout<<endl;
}
return 0;
7. Page 6 of 10
}
Pattern 7
Explanation of the logic
Now analyze this pattern. See the previous
pattern and try to analyze that stars are
replaced by spaces and spaces by stars. So if
you do that, you will get following pattern:
i.e. we will get half the required code on left
side. So we just need to copy paste that code
of star printing to right side also to get the full
pattern. It will be clear when you will run the
program.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
for(row=1;row<=5;row++)
{
for(col=4;col>=row;col--) //printing no of stars
for each row
{
cout<<"*";
}
for(col2=1;col2<=(row*2)-1;col2++)//printing
no of spaces for each row
{
cout<<" ";
}
for(col=4;col>=row;col--) //printing no of stars
for each row
8. Page 7 of 10
{
cout<<"*";
}
cout<<endl;
}
for(row=1;row<=4;row++)
{
for(col=1;col<=row;col++) //printing no of
stars for each row {
cout<<"*";
}
for(col2=7;col2>=(row*2)-1;col2--) //printing
no of spaces each row {
cout<<" ";
}
for(col=1;col<=row;col++) //printing no of
stars each row
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
Pattern 8
Explanation of the logic
The logic is same as that of Pattern 4,
difference is that here we are printing
alternate ‘#’ and ‘*’.
So to do that we are going to create a variable
named “count=1”, now if this count will be
odd then star will be printed and if this count
will be even then hash will be printed. This
will be more clear when you see the following
code.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col,col2;
int count=1;
for(row=1;row<=5;row++)
{
9. Page 8 of 10
for(col=4;col>=row;col--) //printing no of
spaces for each row
{
cout<<" ";
}
for(col2=1;col2<=row;col2++)
{
if(count%2==0) //printing alternate star and
hash
cout<<"# ";
else
cout<<"* ";
count++;
}
cout<<endl;
}
return 0;
}
Pattern 9
Explanation of the logic
This is Pascal’s triangle, which can be printed
using combinations as seen above. This will be
more easy if we print this pattern using
recursion.
Program
#include <iostream>
using namespace std;
int fact(int n)
{
if(n==0|n==1)
return 1;
else
return n*fact(n-1);
}
int nCr(int n, int r)
{
return fact(n)/(fact(r)*fact(n-r));
}
int main()
{
int row,col,col1;
for(row=0;row<5;row++)
{
10. Page 9 of 10
for(col=5-row;col>0;col--){
cout<<" ";
}
for(col1=0;col1<=row;col1++){
cout<<" "<<nCr(row,col1);
}
cout<<endl;
}
return 0;
}
Pattern 10:
Explanation of the logic
This is similar to pattern 1 printing; we just
need to do some minor changes in program.
So here we have to print column%2. This will
be more clear when you will see the following
code.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col;
;
for(row=1;row<=5;row++)
{
for(col=1;col<=row;col++)
{
int remainder=col%2;
cout<<remainder;
}
cout<<endl;
}
return 0;
}
11. Page 10 of 10
Pattern 11
Explanation of the logic
Here we need to print column*row.
Program
#include <iostream>
using namespace std;
int main()
{
int row,col;
;
for(row=1;row<=5;row++)
{
for(col=1;col<=row;col++)
{
int print=col*row;
cout<<print;
}
cout<<endl;
}
return 0;
}