C++ question using control structures, sequences, selection and repetition C++ q
ID: 3841286 • Letter: C
Question
C++ question using control structures, sequences, selection and repetition
C++ question that relates to the question above, please help
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: 12321 e 55555 45554 11611 e Write a program that reads in a five-digit integer and determines whether it's a palindrome. The sample output is provided below. Hint: Use the division and modulus operators to separate the number into its individual digits. Enter a five-digit integer 1 to quit): 12 The number 12321 is a palindrome Enter a five-digit integer (or 1 to quit): 12345 The number 12345 is not a palindrome. Enter a five-digit integer (or 1 to quit): 123 The number 123 is not a five-digit number. Enter a five-digit integer (or 1 to quit): -1 Good bye!Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
void isPalindrome(int value)
{
string textValue = to_string(value) ;
if(textValue.length()<5)
{
cout<<"The number "<<value <<" is not a five-digit number."<<endl;
return;
}
int tempFront = value;
int tempBack = value;
int divider = 10000;
int i=0;
while(i<3)
{
if(tempFront/divider != tempBack%10)
{
cout<<"The number "<<value <<" is not a palindrome."<<endl;
return;
}
else
{
tempFront = tempFront%divider;
divider = divider/10;
tempBack = tempBack/10;
}
i++;
}
cout<<"The number "<<value <<" is a palindrome!"<<endl;
}
int main() {
while(true)
{
int number;
cout<<" Enter a five-digit number (or -1 to quit): ";
cin>>number;
if(number == -1)
exit(0);
isPalindrome(number);
}
return 0;
}
OUTPUT:
PROGRAM CODE:
#include <iostream>
using namespace std;
void isPalindrome(string value)
{
int i=0, j=value.length()-1;
while(i<value.length())
{
while(value.at(i) == ' ')
i++;
while(value.at(j) == ' ')
j--;
if(value.at(i) != value.at(j))
{
cout<<"The word "<<value <<" is not a palindrome."<<endl;
return;
}
i++;
j--;
}
cout<<"The word "<<value <<" is a palindrome!"<<endl;
}
int main() {
while(true)
{
string number;
cout<<" Enter a string (or -1 to quit): ";
getline(cin,number);
if(number == "-1")
{
cout<<"Good bye!";
exit(0);
}
isPalindrome(number);
}
return 0;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.