Palindrome Program in C++

Palindrome Program

This post describes the concept of the palindrome program in c++ using the coding for palindrome numbers. It also explains what is a palindrome and its logic for programming.

What is Palindrome?

Those words and number that are same from backward or forward when read is called palindrome number. For Examples: (numbers: 121,1221,4334,676 ),(word: madam, anna, radar, level) etc.

Logic for palindrome number program:

  1. Get the input number from the user.
  2. Store the number in the temporary variable.
  3. Repeat the process mentioned in step 4 till the number entered is not equal to zero.
  4. Three calculations are implemented to reverse the number:
    • r=num%10 (to find the remainder(r) from the number where num is the temporary variable in which number is stored).
    • rev=(rev*10)+r; (the remainder is added to the new calculating number (rev i.e. a reverse number) whose initial value is zero.
    • num=num/10; (This is used to find the quotient.
  5. Finally, after all the process is done the reverse number is checked whether it is equal to the original number or not.
    • if it is equal to an original number then no. is a palindrome
    • Otherwise, if it is not equal then the number is not a palindrome.

Coding of Palindrome number in C++

Palindrome program using while loop in C++

This palindrome program is implemented using a while loop and the input is taken by the user to check whether the number entered by the user is palindrome or not.

Code using while loop using C++

#include<iostream>
using namespace std;
int main()
{
    int n,n1,r,rev=0;
    cout<<"\n Enter The Number:";
    cin>>n;
    n1=n;
     while(n>0)
    {
        r=n%10;
        rev=rev*10+r;
        n=n/10;
    }
     if(n1==rev)
    {
        cout<<n1<<" is a Palindrome Number";
    }
    else
    {
        cout<<n1<<" is not a Palindrome Number";
    }
 return 0;
}

Output:

Enter the Number:121
121 is a Palindrome Number

Palindrome number in C++ using function

Now, we will understand the code using the function to check if the number entered by the user is palindrome or not.

Code using function in C++

#include<iostream>
using namespace std;
int palindrome(int num)
{
int num1,d,rev=0;
num1=num;
while(num!=0)
{
d=num%10;
rev=(rev*10)+d;
num=num/10;
}
if(rev==num1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int num2,rev1;
cout<<"enter no.\n";
cin>>num2;
rev1=plaindrome(num2);
if(rev1==1)
{
cout<<"it is palindrome no.";
}
if(rev1==0)
{
cout<<"it is not palindrome no.";
}
return 0;
}

Output:

enter no.
121
it is palindrome no.

Palindrome using recursion in C++

In the following coding, we will be the recursion method to check whether the number is palindrome or not.

Code using recursion in c++

#include <iostream> 
using namespace std; 
 int rev(int n, int temp) 
{ 
     if (n == 0) 
        return temp; 
     temp = (temp * 10) + (n % 10); 
    return rev(n / 10, temp); 
} 
  
int main() 
{ 
  
    int n ;
    cout<<"enter a number";
   cin>>n; 
    int temp = rev(n, 0); 
    if (temp == n) 
        cout << "yes" << endl; 
    else
        cout << "no" << endl; 
    return 0; 
} 

Output:

enter the number 141
yes

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments