Could you please solve this C++ question without using void & string functions &
ID: 3841423 • Letter: C
Question
Could you please solve this C++ question without using void & string functions & only and only by using if else, while, for?
This C++ problem related to the previous one, and please somebody solve it without using void and string fuctions, and only and only by using if else, while, for
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
The two programs are as follows:
The first one is for the five digit number:
#include<iostream.h>
int main(){
int input; //holds the input number
int rev = 0; // holds the reverse number
int num; //holds the input number for calculation
cout << "Enter a five-digit integer (or -1 to quit)";
cin >> input;
if (input == -1){
cout << "Good bye!";
return;
}
num = input;
while (num != 0){
rem = num % 10;
rev = rev*10 + rem; //In this while loop reverse number is getting build up
num = num / 10;
}
if (rev == input)
cout << "The number " << input << "is a palindrome!" << endl;
else
cout << "The number " << input << "is not a palindrome." << endl;
return 0;
}
The second program is as follows:
#include<iostream.h>
using namespace std;
int main(){
char input[256]; //holds the input string. Assuming it takes 256 characters.
char rev[256]; // holds the reverse string
char temp[256] //holds the input string without spaces
int found;
int statement; // holds whether input is a word or statement. For word it is 0 and for statement it is 1
cout << "Enter a string (or -1 to quit)";
cin.getline(input,256);
if (input[0] == '-' && input[1] == '1'){
cout << "Good bye!";
return 0;
}
statement = 0;
count = 0;
for (int i = 0; input[i] != ' '; i++){
count++;
if (input[i] != ' ')
temp[i] = input[i]; //building a string without spaces
else
statement = 1;
}
for (int i = count-1; i > 0; i--){
rev[count-1-i] = temp[i]; //building a reverse string
found = 1 //Assume it is a palidrome
for (int i = 0; i<count; i++){
if (rev[i] != temp[i]){
found = 0;
break;
}
}
if (found == 1){
if (statement == 1)
cout << "The statement " << temp << "is a palindrome!" << endl;
else
cout << "The word " << temp << "is a palindrome!" << endl;
}
else {
if (statement == 1)
cout << "The statement " << temp << "is not a palindrome." << endl;
else
cout << "The word " << temp << "is not a palindrome." << endl;
}
return 0;
}