Categories: C++

Check for palindrome number

A palindrome number is a number that remains the same when digits are reversed. For example, the number 125521 is a palindrome number, but 1451 is not.

Program Logic:

  • First, Declare a variable reverseNum and initialize it with 0.
  • Now, make a while loop till the original number is greater than zero.
  • In every loop, get the last digit of the number and add that digit at the end of the reverseNum and then, divide the original number by 10.
reverseNum = reverseNum * 10 + (number % 10)
  • Lastly, check if the original number and reverseNum number are equal or not.

Program code:

#include<bits/stdc++.h>
using namespace std;

void check_palin(int n){
    int temp=n;
    int reverseNum=0;
    do
     {
         int r = temp % 10;
         reverseNum = (reverseNum * 10) + r;
         temp = temp / 10;
     } while (temp != 0);

    if(n==reverseNum){
        cout<<n<<" is a palindrome number";
    }
    else{
        cout<<n<<" is not a palindrome number";
    }
}

int main(){
    int num;
    cout<<"Enter the number: ";
    cin>>num;
    check_palin(num);
    return 0;
}
Output:
Enter the number: 112232211
112232211 is a palindrome number

Note: also read about Check for Prime number

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

5 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

11 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago