(Palindromes) A palindrome is a number or a text phrase that reads the same backward as forward.
For example, each of the following five digit integers is a palindrome: 1 2321 , 55555, 45554 and 1 1 61 1 . Write a program that reads in a five digit integer and determines whether it’s a palindrome.
[Hint: Use the division and modulus operators to separate the number into its individual digits.]
#include<>
#include
using namespace std;
int main()
{
//declare
int x;
int a;
int b;
int l;
int m;
//ask
cout << "Please enter the 5 digit number: ";
cin >> x;
//calculate digits
a = x / 10000;
b = x % 10;
l = (x / 1000) %10;
m = (x % 100) /10;
if( (a == b) & & (l == m) )
cout << "Entered number is palindrome! " << endl;
else
cout << "Entered number is NOT palindrome! " << endl;
//for pause
system("PAUSE") ;
return main() ;
}
For example, each of the following five digit integers is a palindrome: 1 2321 , 55555, 45554 and 1 1 61 1 . Write a program that reads in a five digit integer and determines whether it’s a palindrome.
[Hint: Use the division and modulus operators to separate the number into its individual digits.]
Solution:
#include<iostream>#include<>
#include
using namespace std;
int main()
{
//declare
int x;
int a;
int b;
int l;
int m;
//ask
cout << "Please enter the 5 digit number: ";
cin >> x;
//calculate digits
a = x / 10000;
b = x % 10;
l = (x / 1000) %10;
m = (x % 100) /10;
if( (a == b) & & (l == m) )
cout << "Entered number is palindrome! " << endl;
else
cout << "Entered number is NOT palindrome! " << endl;
//for pause
system("PAUSE") ;
return main() ;
}
Post A Comment:
0 comments: