Find given number is Palindrome or not
A number is said to a palindrome if the reverse of the number is same as the original number.
This can be explained clearly using an example
Example: If the given input is 131, now when we reverse the number then we get the same number as output.Hence, the number is said to a palindrome.
This can be explained clearly using an example
Example: If the given input is 131, now when we reverse the number then we get the same number as output.Hence, the number is said to a palindrome.
INPUT:131
OUTPUT: The number is palindrome
Example: If the given input is 178, now when we reverse the number then output is not same as the given input.Hence, the number is not a palindrome.
INPUT:178
OUTPUT: The number is not a palindrome
Program:
/*Program to find the given number is palindrome or not*/
| #include <iostream.h> |
using namespace std;
int main()
{
int n,r,s=0,t;
cout<<"Enter the number: ";
cin>>n; //takes the input
t = n; //storing the input in a temporary variable
while(n>0)
{ //logic to reverse the given number
r = n % 10;
s = (s * 10) + r;
n = n / 10;
}
if(t == s)
cout<<" The given number "<<t<<" is Palindrome.";
else
cout<<"The given number "<<t<<" is not Palindrome.";
return 0;
}
OUTPUT:
| Enter the number: 575 |
The given number 575 is Palindrome
Program description:Initially we will include the header files required for the program. A namespace is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, one can define the context in which names are defined. cout is used to print the given data on the display screen. cin is used to accept the given input. The given input is stored in a variable n, this given input is stored in an temporary variable t. while loop is used to reverse the given number.
No comments:
Post a Comment