Hacker Challenge: Palindromic Pyramids
Let's create different pyramid patterns using nested loops.
We'll cover the following
In this lesson, we will write a program that prints pyramids with numbers and alphabets. So let’s start a discussion on the pyramid of numbers!
Pyramid of numbers
Write and run a program that prints the following shape:
Height: 5
1
121
12321
1234321
123454321
Look at the above example for Height: 5
, where we need to figure out the pattern in each line. Notice that the numbers are printed in ascending order and descending order from 1
to a number k
.
- In line 1, one number is printed.
- In line 2, two numbers are printed in ascending order and one in descending order.
- In line 3, three numbers are printed in ascending order and two in descending order.
Can you see a pattern based on the line number?
Pyramids of numbers
How many numbers will be printed in ascending and descending order if we have the nth line?
Let’s see the above pattern in a table.
Pyramid of numbers
Line # | Space | Numbers in ascending order | Numbers in descending order |
1 | height - 1 | 1 | 0 |
2 | height - 2 | 2 | 1 |
3 | height - 3 | 3 | 2 |
. | . | . | . |
. | . | . | . |
ln | height - ln | ln | ln - 1 |
Idea
- For each line
ln
from1
toh
:- Print spaces following the spaces pattern
h-ln
. - Print numbers in ascending order from
1
toln
. - Print numbers in descending order from
ln-1
to1
.
- Print spaces following the spaces pattern
Let us implement the above idea into the program.
int num=1; //initially num assign with 1for (int ln = 1; ln <= h; ln++) // print triangle till height{// Spaces part:for (int sp = 1; sp <= h - ln; sp++)cout << " "; //print spaces in each line// Ascending part: from 1 ... lnfor (int n = 1; n <= ln; n++) //Run the loop till line numbercout << n; // print the number// Descending part: from ln-1 ... 1for (int n = ln-1; n >=1 ; n--)cout << n; //print the numbercout << endl; //move to the next line}
Let’s write the above code in a function and see the output:
#include <iostream> using namespace std; void numPyramid(int h) { int num = 1; for (int ln = 1; ln <= h; ln++) { for (int sp = 1; sp <= h - ln; sp++) cout << " "; for (int sym = 1; sym <= ln; sym++) { cout << num; num++; } num--; for (int sym = 1; sym <= ln-1; sym++) { num--; cout << num; } cout << endl; } } int main() { numPyramid(5); return 0; }
Let us build the pyramid of alphabets now.
Pyramid of alphabets
Write and run the program that prints the following pyramid:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Let’s think about what changes we need to make in the above program to solve this problem.
-
We have not been provided with the height of the pyramid. Instead, we have been provided with the base center of the pyramid, which will be the exact height of the pyramid. For example, if we have the alphabet
'D'
, which is the fourth letter, that meansh=4
. Similarly, if we have the letter'Z'
, that meansh=26
. We need to print the alphabets in ascending and descending order depending upon which line we are printing. -
We need to print the alphabets in ascending and descending order depending upon which line we are printing.
Quiz
Before looking at the problem, let us take a small quiz to remind ourselves how arithmetic works on the character type.
What will be the output of the following piece of code:
char sym = 'A';
sym++;
cout << sym<<endl;
Syntax Error
B
Let’s see what changes we have done in the above program:
#include <iostream> using namespace std; void alphaPyramid(char s) { int h = s - 'A' +1; // calculate the height of triangle for (int ln = 1; ln <= h; ln++) // run h times { for (int sp = 1; sp <= h - ln; sp++) // Run the loop till h-ln cout << " "; char symbol = 'A'; // initially we have A // Printing Alphebets: from 'A' till ln'th alphabet for (int c = 1; c <= ln; c++)// for ln characters { cout << symbol; // Print alphabet symbol++; // increment in ASCII, 'B' then 'C' and so on } symbol--; symbol--; // decrement in ASCII value twice for (int c = 1; c <= ln-1; c++) // Run the loop till line - 1 { cout << symbol; // Print alphabet symbol--; // decrement in ASCII value } cout << endl; } } int main() { alphaPyramid('E'); // 'E' as the base center. return 0; }
Line 6: We calculate the height of the triangle, by subtracting 'A'
from s
and adding 1
to it; for example, if s='E'
, then h = s - 'A' +1
. This is equal to 5.
Lines 12 and 16–17: We initialize the symbol to ‘A’ and, inside the loop, it increments the symbol by 1
. Hence, the loop condition c<=ln
makes sure that it prints ln alphabets first.
Line 19: The symbol holds 1
alphabet larger than the last character when the loop ends. For example, if ln=3
, then the symbol
will hold 'D'
, and the console has already been written with ABC
. Hence, there are two decrements, so that now for printing in descending order, the symbol should hold 'B'
.
Lines 22–23: We print the symbol and decrement the ASCII value.