CPP All : Pattern printing loops
Concept Learning Code Sheets

#### Pattern – Inverted right half triangle of symbol #2464

Printing the inverted right half triangle made of same symbol as per row count given by the user.

#include <iostream>
using namespace std;
int main()
{
int cnt;
cout<<"Give row count ";
cin>>cnt;
for(int i=1;i<=cnt;i++)
{
for(int j=cnt;j>=i;j--)
{
cout<<"*";
}
cout<<"n";
}
}

#### Pattern – Upright right half triangle of symbol #2470

Printing the upright right half triangle made of same symbol as per row count given by the user.

#include <iostream>
using namespace std;
int main()
{
int cnt;
cout<<"Give row count ";
cin>>cnt;
for(int i=1;i<=cnt;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<"n";
}
}

#### upright triangle with user given character #2482

Printing an upright triangle filled with user given character.

#include<iostream>
using namespace std;
int main()
{
int i,j,k,r;
char ch;
cout<<"Give row count :";
cin>>r;
cout<<"Give character :";
cin>>ch;
for(i=1;i<=r;i++)
{
for(j=r;j>i;j--) cout<<' ';
for(k=1;k<2*i;k++) cout<<ch;
cout<<endl;
}
return 0;
}

#### Inverted triangle with user given character #2804

Printing an inverted triangle filled with user given character.

#include<iostream>
using namespace std;
int main()
{
int i,j,k,r;
char ch;
cout<<"Give row count :";
cin>>r;
cout<<"Give character :";
cin>>ch;
for(i=1;i<=r;i++)
{
//for(j=r;j>i;j--)
for(j=0;j<i-1;j++)
cout<<' ';
//for(k=1;k<2*i;k++)
for(k=(r-i+1)*2;k>1;k--)
cout<<ch;
cout<<endl;
}
return 0;
}

#### Upright right half triangle of numbers #2809

Print the right half upright pyramid of increasing numbers.

#include <iostream>
using namespace std;
int main()
{
int cnt;
cout<<"Give row count : ";
cin>>cnt;
for(int i=1;i<=cnt;i++)
{
for(int j=1;j<=i;j++)
cout<<j<<" ";
cout<<"n";
}
}

#### Upright right half triangle of alphabets (column-wise increasing) #2814

Print the right half upright pyramid of column wise increasing alphabets like follows –
A
A B
A B C

#include <iostream>
using namespace std;
int main()
{
int cnt;
cout<<"Give row count : ";
cin>>cnt;
for(int i=1;i<=cnt;i++)
{
for(int j=1;j<=i;j++)
{
cout<<char(j+64)<<" ";
}
cout<<"n";
}
return 0;
}

#### Upright right half triangle of alphabets (row-wise increasing) #2854

Print the right half upright pyramid of row wise increasing alphabets like follows –
A
B B
C C C
…………..

#include <iostream>
using namespace std;
int main()
{
int cnt;
cout<<"Give row count : ";
cin>>cnt;
for(int i=1;i<=cnt;i++)
{
for(int j=1;j<=i;j++)
{
cout<<char(i+64)<<" ";
}
cout<<"n";
}
return 0;
}

#### Printing the Flyod’s Triangle #2856

Printing the Flyod’s triangle of numbers which are increasing in both row and column wise fashion

#include <iostream>
using namespace std;
int main()
{
int cnt;
int k=1;
cout<<"Give row count: ";
cin>>cnt;
for(int i=1;i<=cnt;i++)
{
for(int j=1;j<=i;j++)
{
cout<<k<<" ";
k++;
}
cout<<endl;
}
return 0;
}

#### Upright Hollow Triangle with given base size #3111

Printing upright hollow triangle with given base size.

#include <iostream>
using namespace std;
int main()
{
int n=5;
int i,j,k=0;
int sp;
int g=n;
for (i=1; i<n+3; i++)
{
for (sp=g; sp>=0; sp--) cout << " ";
for (j=1; j<k; j++)
{
if(j==1 || j==(k-1) || i==(n+2)) cout << "*";
else cout << " ";
}
cout << endl;
k+=2;
g--;
}
return 0;
}

#### Downward Hollow Triangle with given base size and left offset #3116

Printing downward hollow triangle with given base size and left offset.

#include <iostream>
using namespace std;
int main()
{
int n=5;
int i,j,k=n*2+1;
int sp;
int g=2;
for (i=1; i<n+3; i++)
{
for (sp=1; sp<=g; sp++) cout << " ";
for (j=k; j>=1; j--)
{
if(j==k || j==1 || i==1) cout << '*';
else cout << ' ';
}
cout << endl;
k-=2;
g++;
}
return 0;
}

#### Printing a diamond pattern #3124

A program that prints a diamond pattern of stars where a size unit is given in form of half of the diamond height.

#include <iostream>
using namespace std;
int main()
{
int n=5;
int i,j;
int sp=n-1;
for(int i=1;i<=n;++i)
{
for (j = 1; j<=sp; j++) cout<<" ";
sp--;
for (j=1;j<=2*i-1;j++) cout<<"*";
cout<<endl;
}
sp=1;
for(int i=1;i<=n-1;++i)
{
for (j=1;j<=sp;j++) cout<<" ";
sp++;
for (j=1;j<=2*(n-i)-1;j++) cout<<"*";
cout<<endl;
}
return 0;
}

#### Printing a damru/dumbbell pattern #3128

A program that prints a damru or dumb bell pattern of stars where a size unit is given in form of half of the damru height.

#include <iostream>
using namespace std;
int main()
{
int n=5;
int i,j;
int sp=0;
for(int i=0;i<=n-1;++i)
{
for (j=1;j<=sp;j++) cout<<" ";
sp++;
for (j=1;j<=2*(n-i)-1;j++) cout<<"*";
cout<<endl;
}
sp=n-1;
for(int i=1;i<=n;++i)
{
for (j=1;j<=sp; j++) cout<<" ";
sp--;
for (j=1;j<=2*i-1;j++) cout<<"*";
cout<<endl;
}
return 0;
}

#### Pascal’s Triangle Printing #3173

Printing a pascal’s triangle of given number of rows.

#include <iostream>
using namespace std;
int main()
{
int r;
cout<<"Enter number of r: ";
cin>>r;
int m=1;
for(int i=0;i<r;++i)
{
for(int sp = 1; sp <= r-i; sp++)
cout <<" ";
for(int j=0;j<=i;j++)
{
if (j==0||i==0)m=1;
else m = m*(i-j+1)/j;
cout<<m<< " ";
}
cout << endl;
}
return 0;
}
Solved Problems

#### Triangular Number series using the Flyod’s triangle method #2860

Write a program to print a series of triangular numbers starting 1 using the

Code Sheets:13  Solved Problems:1
×