Question 2 A palindrome number is one that reads the same backward as forward. F
ID: 3705280 • Letter: Q
Question
Question 2 A palindrome number is one that reads the same backward as forward. For example, each of the following integers is a palindrome: 9123219, 5555, 678876, 45554 and 161 Write a program that reads in a positive integer with minimum 2 digits and maximum 9 digits and determines whether it is a palindrome If the input is invalid, please keep on asking until you get the correct input Example 1: Bold means user input. Enter a positive integer with minimum 2 digits and maximum 9 digits: 6 Invalid input. Try again. Enter a positive integer with minimum 2 digits and maximum 9 digits: 1234567890 Invalid input. Try again Enter a positive integer with minimum 2 digits and maximum 9 digits: 6210126 Your integer 6210126 is palindrome Example 2: Bold means user input. Enter a positive integer with minimum 2 digits and maximum 9 digits: -100 Invalid input. Try again. Enter a positive integer with minimum 2 digits and maximum 9 digits: 12345 Your integer 12345 is not palindrome Hint 1: If needed, you can use the following function to determine the number of digits in an integer. // You need to declare #include«cmath» // in order to use this function int getNoOfDigit(int num) return int (log10 (num)) + 1; Hint 2: If needed, you can use the built-in function pow(base, exponent), which is also in the library For example, pow(1e, 3) will give you the value 1000Explanation / Answer
program
#include<stdio.h>
#include<math.h>
int main()
{
int n, rev = 0, rem, number,digit;
printf("Enter an integer with minimum 2 digits and maximum 9 digits: ");
scanf("%d", &n);
if(n<0)
{
printf("Invalid input. Try again");
return ;
}
digit= (log10(n))+1;
if(digit<2 )
{
printf("Invalid input. Try again");
return ;
}
if(digit>9 )
{
printf("Invalid input. Try again");
return ;
}
number = n;
while( n!=0 )
{
rem = n%10;
rev = rev*10 + rem;
n /= 10;
}
if (number == rev)
printf("%d is a palindrome.", number);
else
printf("%d is not a palindrome.", number);
return 0;
}
output
Enter an integer with minimum 2 digits and maximum 9 digits: 454
454 is a palindrome.
Enter an integer with minimum 2 digits and maximum 9 digits: -9
Invalid input. Try again
Enter an integer with minimum 2 digits and maximum 9 digits: 9877875565
Invalid input. Try again
Enter an integer with minimum 2 digits and maximum 9 digits: 6744
6744 is not a palindrome.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.