Friday, June 18, 2021
Home coding Write a program to check whether number is palindrome or not.

Write a program to check whether number is palindrome or not.

Given a number we have to check number is palindrome or not.

so what is Palindrome number?

Palindrome number is a number which is same whether we read it from backward or forward.

for example-11,22,33,121,99 all are palindrome number

Example

input:  11
output: true

input: 112
output:false

input:121
output:true

Logic:

Logic behind this question is to just check the reverse of number . if reverse of number is equal to original number then it is palindrome .otherwise not.

#include<iostream>
using namespace std;
int main()
{
 int num;
 cout<<"Enter a number  : "<<endl;
 cin>>num;
 int reverse_number=0;
 int original_number=num;
 int pal=0;
 while(num>0)
 {
     int rem=num%10;
     reverse_number=reverse_number*10+rem;
     num=num/10;
 }
 if(reverse_number==original_number)
 {
     cout<<original_number<<" is palindrome number "<<endl;
 }
 else
 {
     cout<<"number is not palindrome"<<endl;
 }
 

}

Output

Enter a number  : 11
 11 is palindrome number 

Time Complexity-O(N),where N is Number.

Space Complexity-O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments