Printing Palindrome Pattern in C++

Patterns are often considered an effective method to learn logic building and acquire expertise in manipulating loops. Printing a palindrome pattern is amongst the favorite questions of an interviewer. To counter that, in this article, we will discuss palindrome patterns of letters and numbers.

To understand palindrome patterns, we should be aware of the meaning of palindrome. If we look at the formal definition, a palindrome is a word, phrase, or sequence that reads the same backward as forward. An example would be madam, and if we consider numbers, an example could be 121, 12321, and so on.

Palindrome Half Pyramid

Let’s first start with palindrome numbers. The logic is straightforward, we will be using 3 loops to do this.

/* C++ program for Palindrome pyramid pattern printing using numbers */
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n;
cout << "Enter the number of rows : ";
cin >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
cout << j << " ";
}
for(int k = i-1; k >= 1; k--)
{
cout << k << " ";
}
cout << endl;
}
return 0;
}

The output of the following code would be as follows:

number pyramid
number pyramid

The outer loop that iterates using the variable i is used to determining the number of rows. The loop j iterates until the value of j are less than or equal to I, and so the loop i will print a right-angled triangle with numbers increasing by each row. The loop k will complete each row by iterating backward, thus creating a palindrome pattern.

Now the same thing can be done with letters and to demonstrate that we will look at a code.

/* C++ program for Palindrome pyramid pattern printing using numbers */
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n;
cout << "Enter the number of rows : ";
cin >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
 
cout << char(j+65-1) << " ";
}
for(int k = i-1; k >= 1; k--)
{
cout << char(k+65-1)k << " ";
}
cout << endl;
}
return 0;
}

We use the same code as above for letters, and instead of printing integer variables, we add ASCII values to those integers to print characters. The output would be the same as above and include capital letters because 65 is the ASCII for capital A.

Palindrome Pyramid

Let’s see an example that will print the whole pyramid like an equilateral triangle.

#include <iostream>
using namespace std;
int main()
{
int k, n;
cout << "Enter the number of rows : ";
cin >> n;
cout << " ";
for (int i=1; i<=n; i++)
{

for (int j=1; j<=n-i; j++)
cout << " ";

for (j=1,k=2*i-1; j<=2*i-1; j++,k--)
{
if (j <= k)
cout << j;
else
cout << k;
}
cout << endl;

cout << " ";

}
return 0;
}

Output:

pyramid
pyramid

Apart from the palindrome code demonstrated above there are other types of palindromes like the pascal’s triangle.

#include <iostream>
using namespace std;
//pascal's triangle
int main()
{
    int rows, coefficient = 1;

    cout << "Enter number of rows: ";
    cin >> rows;

    for(int i = 0; i < rows; i++)
    {
      // printing space
        for(int space = 1; space <= rows-i; space++)
            cout <<"  ";

        for(int j = 0; j <= i; j++)
        {
            if (j == 0 || i == 0)
                coefficient = 1;
            else
                coefficient = coefficient*(i-j+1)/j;

            cout << coefficient << "   ";
        }
        cout << endl;
    }

    return 0;
}

The output of the code will be as follows:

pascal's triangle
pascal’s triangle

Pascal’s triangle is a famous pattern in mathematics. Pascal’s triangle is used to find combinations and uses the concept of binomial coefficients.

Palindrome String

To check whether a string is a palindrome or not

  • input the string that has to be checked
  • initialize an array of characters to store the reverse of the string
  • traverse the array of characters and store the string in a reverse order
  • compare the character array and the input string
  • if the result is true then the string is a palindrome otherwise its not
#include <iostream>
//This header file is used to make use of the system defined String methods.
#include <string.h>

using namespace std;

int main()
{

    //String Variable Declaration
    char s1[100], c = 'a';
    int n1, i = 0;

    cout << "\n\nEnter the String you want to check : ";
    cin >> s1;

    //Computing string length without using system defined method
    while (c != '\0')
    {
        c = s1[i++];
    }

    n1 = i-1;
    char s2[n1+1];

    cout << "Length of the entered string is : " << n1 << endl;

    i = 0;
    //Computing reverse of the String without using system defined method
    while (i != n1 + 1)
    {
        s2[i] = s1[n1 - i - 1];
        i++;
    }

    cout << "Reverse of the entered string is : " << s2 << endl;

    i = 0;
    //Logic to check for Palindrome
    while (i != n1)
    {
        if (s2[i] != s1[i])
          //the loop breaks
            break;

        i++;
    }

    if (i != n1)
        cout << "The String \"" << s1 << "\"" << " is not a Palindrome.";
    else
        cout << "The String \"" << s1 << "\"" << " is a Palindrome.";

    cout << "\n\n";

    return 0;
}

We can also use a library function called a reverse to reverse the string for the above solution. Another way of checking string palindrome is traversing the same string from the last index and comparing it with the actual string. This method is demonstrated below.

#include <iostream>
using namespace std;

int main() {
    char string1[20];
    int i, length;
    int flag = 0;
    
    cout << "Enter a string: "; 
    cin >> string1;
    //calculate the length of the string using a built in function
    length = strlen(string1);
    
    for(i=0;i < length ;i++){
        if(string1[i] != string1[length-i-1]){
          //flag variable set to true
            flag = 1;
            break;
   }
}
    
    if (flag) {
        cout << string1 << " is not a palindrome" << endl; 
    }    
    else {
        cout << string1 << " is a palindrome" << endl; 
    }
    
    return 0;
}

Palindrome Number

Checking for a palindrome number is easy and involves breaking the number and reversing it.

#include <iostream>
using namespace std;

int main()
{
     int n, num, digit, rev = 0;

     cout << "Enter a positive number: ";
     cin >> num;

     n = num;

     do
     {
         digit = num %% 10;
       // reversing the number
       //we first need to multiply the current data in the rev variable by 10 in order to add the digit to the nth place in the number.
         rev = (rev * 10) + digit;
         num = num / 10;
     } while (num != 0);

     cout << " The reverse of the number is: " << rev << endl;
//comparing the orignal and the reversed number
     if (n == rev)
         cout << " The number is a palindrome.";
     else
         cout << " The number is not a palindrome.";

    return 0;
}

Conclusion

A palindrome is an important concept and can involve patterns, string, and numbers. It is a concept that is widely tested in exams and interviews. We have written the possible approach to each sort of problem. However, there are still many other ways to solve these problems. Stay tuned to read more similar types of articles.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *