This is a C Program which reverses a number & checks if it is a palindrome or not.
Problem Description
This C program accepts an integer, reverse it and also checks if it is a palindrome or not.
Problem Solution
1. Take the number which you have to reverse as the input.
2. Obtain its quotient and remainder.
3. Multiply the separate variable with 10 and add the obtained remainder to it.
4. Do step 2 again for the quotient and step 3 for the remainder obtained in step 4.
5. Repeat the process until quotient becomes zero.
6. When it becomes zero, check if the reversed number is equal to original number or not.
7. Print the output and exit.
Program/Source Code
Here is source code of the C program to reverse a number & checks it is a palindrome or not. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include
void main()
{
int num, temp, remainder, reverse = 0;
printf(“Enter an integer \n”);
scanf(“%d”, &num);
/* original number is stored at temp */
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf(“Given number is = %d\n”, temp);
printf(“Its reverse is = %d\n”, reverse);
if (temp == reverse)
printf(“Number is a palindrome \n”);
else
printf(“Number is not a palindrome \n”);
}
Runtime Test Cases
Case:1
Enter an integer
6789
Given number is = 6789
Its reverse is = 9876
Number is not a palindrome
Case:2
Enter an integer
58085
Given number is = 58085
Its reverse is = 58085
Number is a palindrome