Exercise:
Write C++ programs to create different patterns and pyramids.
Example 1: Printing half pyramid of steric.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row;
cout << "Insert the number of Rows of triangle: ";
cin >> Row;
for(int x = 1; x <= Row; ++x)
{
for(int y = 1; y <= x; ++y)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
Click Here to View the Output:
Insert the number of Rows of triangle: 5
*
* *
* * *
* * * *
* * * * *
Click Here to View the Explanation:
- The user is requested to enter the required number of rows of the half pyramid. The entered number is stored as
Row
. - The value of
x = 1
, andfor
loop begins. This is a nestedfor
loop i.e.for
loop withinfor
loop. - Another
for
loop is added to print the number of steric required for each row. Asx
increases by 1 through every iteration, the value ofy = 1
is set for the second for loop. It also is incremented by 1 through every iteration. The condition is set toy <= x
. - The second loop is exited and a new line is added. The first loop is iterated again until the value of
x <= Row
. - A pyramid is printed.
Example 2: Printing half pyramid of numbers.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row;
cout << "Insert the number of rows of triangle: ";
cin >> Row;
for(int x = 1; x <= Row; ++x)
{
for(int y = 1; y <= x; ++y)
{
cout << y ;
}
cout << "\n";
}
return 0;
}
Click Here to View the Output:
Insert the number of rows of triangle: 5 1 12 123 1234 12345
Click Here to View the Explanation:
- This code is similar to Example but instead of printing steric, the value of y is printed.
Example 3: Printing pyramid of alphabets.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
char c, letter = 'a';
cout << "Enter the letter which will be the last row: ";
cin >> c;
for(int x = 1; x <= (c-'a'+1); ++x)
{
for(int y = 1; y <= x; ++y)
{
cout << letter << " ";
}
++letter;
cout << endl;
}
return 0;
}
Click Here to View the Output:
Enter the letter which will be the last row: f a b b c c c d d d d e e e e e f f f f f f
Click Here to View the Explanation:
- This code is also similar to Example 1.
- User is requested for a letter that should be printed in the last row.
a
variable letter is declared aschar
type. Through every secondfor
loop, it is incremented by 1, moving on to the next letter.c- 'a' + 1
is used to control the iteration of the firstfor
loop.
Example 4: Printing inverted pyramid of steric.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row;
cout << "Insert the number of Rows of triangle: ";
cin >> Row;
for(int x = Row; x >= 1; --x)
{
for(int y = 1; y <= x; ++y)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
Click Here to View the Output:
Insert the number of Rows of triangle: 4 * * * * * * * * * *
Click Here to View the Explanation:
- This code is again similar to the previous one. This code requires the number of rows to be printed
Row
from the user. x
is assigned the value ofRow
and decremented by 1 through every iteration.- It uses the second
for
loop to control the number of steric to be printed per row.
Example 5: Printing inverted pyramid of numbers.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row;
cout << "Insert the number of rows of triangle: ";
cin >> Row;
for(int x = Row; x >= 1; --x)
{
for(int y = 1; y <= x; ++y)
{
cout << y ;
}
cout << "\n";
}
return 0;
}
Click Here to View the Output:
Insert the number of rows of triangle: 7 1234567 123456 12345 1234 123 12 1
Click Here to View the Explanation:
- This code uses works in the similar way as the previous code.
- Instead of printing steric,
y
is printed through every iteration.y
is incremented through every iteration,x
is decremented through every iteration. - A pyramid is printed.
Example 6: Printing full pyramid of steric.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int gap, Row;
cout <<"Enter the number of rows of triangle: ";
cin >> Row;
for(int x = 1, j = 0; x <= Row; ++x, j = 0)
{
for(gap = 1; gap <= Row-x; ++gap)
{
cout <<" ";
}
while(j != 2*x-1)
{
cout << "* ";
++j;
}
cout << endl;
}
return 0;
}
Click Here to View the Output:
Enter the number of rows of triangle: 5 * * * * * * * * * * * * * * * * * * * * * * * * *
Click Here to View the Explanation:
- This code requires the user to enter the number of rows.
- The first
for
loop controls the number of rows, the secondfor
loop is to print spaces before the steric start printing. - The
while
loop is added to control the number of steric that should be printed in a row. - The output is a full pyramid of steric.
Example 7: Printing full pyramid of numbers.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row, length = 0, length1 = 0, y = 0;
cout << "Enter the number of Rows of triangle: ";
cin >> Row;
for(int x = 1; x <= Row; ++x)
{
for(int gap = 1; gap <= Row-x; ++gap)
{
cout << " ";
++length;
}
while(y != 2*x-1)
{
if (length <= Row-1)
{
cout << x+y << " ";
++length;
}
else
{
++length1;
cout << x+y-2*length1 << " ";
}
++y;
}
length1 = length = y = 0;
cout << endl;
}
return 0;
}
Click Here to View the Output:
Enter the number of Rows of triangle: 3 1 2 3 2 3 4 5 4 3
Click Here to View the Explanation:
- This code uses different loops to control the printing of the gap before the numbers, the numbers, and the amount of numbers to be printed.
- First
for
loop is to control the number of rows to be printed. - The second
for
loop controls the spaces/gaps before the steric should be printed. while
loop is entered and it starts printing the steric untily ! = 2x-1
.
Example 8: Printing inverted full pyramid of steric.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row;
cout << "Enter the number of Rows of triangle: ";
cin >> Row;
for(int x = Row; x >= 1; --x)
{
for(int gap = 0; gap < Row-x; ++gap)
cout << " ";
for(int y = x; y <= 2*x-1; ++y)
cout << "* ";
for(int y = 0; y < x-1; ++y)
cout << "* ";
cout << endl;
}
return 0;
}
Click Here to View the Output:
Enter the number of Rows of triangle: 4 * * * * * * * * * * * * * * * *
Click Here to View the Explanation:
- This code works in a similar manner to the previous one.
- Instead of incrementing the value of
x
,x
is assigned the value ofRow
and then decremented by 1 through every iteration. - The result is then printed.
Example 9: Printing Pascal’s triangle.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row, Val= 1;
cout << "Enter the number of rows: ";
cin >> Row;
for(int x = 0; x < Row; x++)
{
for(int gap = 1; gap <= Row-x; gap++)
cout <<" ";
for(int y = 0; y <= x; y++)
{
if (y == 0 || x == 0)
Val = 1;
else
Val = Val*(x-y+1)/y;
cout << Val << " ";
}
cout << endl;
}
return 0;
}
Click Here to View the Output:
Enter the number of rows: 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Click Here to View the Explanation:
- First
for
loop controls the number of rows of the pascal’s triangle. - Second
for
loop controls the number of spaces before the numbers have to be printed. - The third
for
loop controls the number that is printed. - In case of first row, 1 has to be printed. An
if... else
statement is used to print 1 ifx == 0
ory == 0
.
Example 10: Printing Floyd’s triangle.
Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
{
int Row, num = 1;
cout << "Enter the number of Rows of triangle: ";
cin >> Row;
for(int x = 1; x <= Row; x++)
{
for(int y = 1; y <= x; ++y)
{
cout << num << " ";
++num;
}
cout << endl;
}
return 0;
}
Click Here to View the Output:
Enter the number of Rows of triangle: 4 1 2 3 4 5 6 7 8 9 10
Click Here to View the Explanation:
- This code requires two
for
loops. Firstfor
loop controls the number of rows. num
is set equal to 1. It is incremented after every iteration of secondfor
loop.- The second
for
loop prints the value ofnum
.