Write a C program to enter any number and check whether given number is palindrome or not using for loop. How to check whether a number is palindrome or not in C programming. Logic to check palindrome number in C program.
Example
Input
Input any number: 121
Output
121 is palindrome
Required knowledge
Basic C programming, If else, For 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
From the definition of the palindrome number. You must first learn to reverse the number to check palindrome. Here I won't explain the logic to find reverse. You can learn it from below link.
You can divide the logic to check palindrome number in three steps.
- Read number from user. Store it in some variable say num.
- Find reverse of the number. Store it in some variable say reverse.
- Compare original number with reversed number. 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; /* Read a number from user */ printf("Enter any number to check palindrome: "); scanf("%d", &n); num = n; //Copy original value to num. /* Find reverse of n and stores in rev */ while(n != 0) { rev = (rev * 10) + (n % 10); n = n / 10; } /* Check if reverse is equal to original num or not */ if(rev == num) { printf("%d is palindrome.", num); } else { printf("%d is not palindrome.", num); } return 0; }
Advance your skills by learning this program using recursive approach.
Enter any number to check palindrome: 121 121 is palindrome.
Happy coding ;)
You may also like
- Loop programming exercises index.
- C program to check whether a string is palindrome or not.
- C program to reverse a given string.
- C program to find frequency of each digit in given number.
- C program to count number of digits in a given number.
- C program to find first and last digit of a given number.
- C program to swap first and last digit of a given number.