C Program to Reverse a Number & Check if it is a Palindrome:
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
Take the number which you have to reverse as the input.
Obtain its quotient and remainder.
Multiply the separate variable with 10 and add the obtained remainder to it.
Do step 2 again for the quotient and step 3 for the remainder obtained in step 4.
Repeat the process until quotient becomes zero.
When it becomes zero, check if the reversed number is equal to original number or not.
Print the output and exit.
....
Show More
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
Take the number which you have to reverse as the input.
Obtain its quotient and remainder.
Multiply the separate variable with 10 and add the obtained remainder to it.
Do step 2 again for the quotient and step 3 for the remainder obtained in step 4.
Repeat the process until quotient becomes zero.
When it becomes zero, check if the reversed number is equal to original number or not.
Print the output and exit.
Program/Source Code
#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");
}
Program Explanation
Take the number which you have to reverse as the input and store it in the variable num.
Copy the input number to the another variable temp.
Firstly initialize the variable reverse to zero.
Obtain the remainder of the input number.
Multiply the variable reverse with 10 and add the Obtained remainder to it and store the result in the same variable.
Obtain the quotient of the input number and considering this as input number repeat the steps as mentioned above until the obtained quotient becomes zero.
When it becomes zero, using if,else statement check whether the reversed number is equal to original number or not.
If it is equal, then print the output as ??Number is a palindrome?, otherwise print the output as ??Number is not a palindrome?.
Output:
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
show less