Loop Exercises
Write a C program to input number from user and check number is palindrome or not using loop. How to check whether a number is palindrome or not using loop in C programming. Logic to check palindrome number in C programming.
Required knowledge
Basic C programming, If else, While loop
What is Palindrome number?
Palindrome number is such number which when reversed is equal to the original number. For example: 121, 12321, 1001 etc.
Logic to check palindrome number
Step by step descriptive logic to check palindrome number.
- Input a number from user. Store it in some variable say num.
- Find reverse of the given number. Store it in some variable say reverse.
- Compare num with reverse. If both are same then the number is palindrome otherwise not.
Program to check palindrome number
/** * C program to check whether a number is palindrome or not */ #include <stdio.h> int main() { int n, num, rev = 0; /* Input a number from user */ printf("Enter any number to check palindrome: "); scanf("%d", &n); /* Copy original value to 'num' to 'n'*/ num = n; /* Find reverse of n and store in rev */ while(n != 0) { rev = (rev * 10) + (n % 10); n /= 10; } /* Check if reverse is equal to 'num' or not */ if(rev == num) { printf("%d is palindrome.", num); } else { printf("%d is not palindrome.", num); } return 0; }
Output
Enter any number to check palindrome: 757
757 is palindrome.