Hacker Challenge: Palindromic Pyramids

Let's create different pyramid patterns using nested loops.

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

Question

How many numbers will be printed in ascending and descending order if we have the nth line?

Show Answer

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 from 1 to h:
    • Print spaces following the spaces pattern h-ln.
    • Print numbers in ascending order from 1 to ln.
    • Print numbers in descending order from ln-1 to 1.

Let us implement the above idea into the program.

Press + to interact
int num=1; //initially num assign with 1
for (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 ... ln
for (int n = 1; n <= ln; n++) //Run the loop till line number
cout << n; // print the number
// Descending part: from ln-1 ... 1
for (int n = ln-1; n >=1 ; n--)
cout << n; //print the number
cout << 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;
}





Pyramid with numbers

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.

  1. 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 means h=4. Similarly, if we have the letter 'Z', that means h=26. We need to print the alphabets in ascending and descending order depending upon which line we are printing.

  2. 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.

1

What will be the output of the following piece of code:

char sym = 'A';
sym++;
cout << sym<<endl;
A)

Syntax Error

B)

B

Question 1 of 30 attempted

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;
}





Pyramid with alphabets

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.