Google Add

Search

C++ Program to Check Whether a Number is Palindrome or Not

Write a c++ program to check whether a number is palindrome or not. In this program, we write a code to take an input number from a user and check whether a number is palindrome or not.

Let's first understand what is a palindrome number?

A palindrome number is a number that remains same when it's digits are reversed.

For example -

i) 121 is a palindrome number.

ii) 12321 is a palindrome number.


In my previous post, I have explained what is palindrome and how to check whether a number

C program to check whether a number is palindrome or not

How to check whether an input number is palindrome or not?


i) Take an input number from a user.

ii) Reverse an input number.

iii) Compare input number and reverse of an input number. If both are equal then entered number is a palindrome number.


C++ Program to Check Whether a Number is Palindrome or Not


C++ Program to Check Whether a Number is Palindrome or Not


#include<iostream>
using namespace std;
  
int main(){

    int num, temp, rev = 0, digit;
    
    //Input number
    cout << "Enter a number \n";
    cin  >> num;
    
    temp = num;
    
    //Reverse a number  
    while (temp > 0) {
        
        digit = temp % 10;   
        rev  = rev * 10 + digit; 
        temp = temp / 10; 
    }
   
    if ( num == rev) {
        cout << num << " is a palindrome number";
    } else {
        cout << num << " is not a palindrome number";
    }
               
    return 0;     
}





C++ program to delete an element from an array

Programming questions on strings

Programming questions on recursion


No comments:

Post a Comment