C programs to check if a number is a palindrome or not have been shown here. For example, 1221 is a palindrome because we get the same number irrespective of the direction (left to right or right to left) of reading. But 1234 is not a palindrome as we get 4321 if we read it from right to left.
The algorithm, time complexity and pseudocode of the program have been shown below.
Page content(s):
1. Algorithm to check if a number is a palindrome or not
1. Take a number n as input.
2. Copy value of n to another variable say m i.e. m = n.
3. Initialize a variable to 0 say reverse = 0.
4. Perform r = n % 10.
5. Perform reverse = reverse * 10 + r.
6. Perform n = n / 10
7. If n = 0 go to step 8 else go to step 3.
8. Check If reverse == m
9. If step 8 is true then n is palindrome else n is not palindrome.
2. Pseudocode to check if a number is a palindrome or not
Input : A number $n$
Output : Palindrome or Not Palindrome
1. Procedure checkPalindrome($n$):
2.
3.
4.
5.
6.
7.
8.
9.
10.
11. End Procedure
3. Time complexity to check if a number is a palindrome or not
Time Complexity: O(log(n))
Where n is the input number.
4. C Program & output to check if a number is a palindrome or not using iteration
/********************************** alphabetacoder.com C program to check if a number is a palindrome or not using iteration ***********************************/ #include <stdio.h> int main() { // declare variables int n, m, revnum = 0, r; // take input of the number printf("Enter the number = "); scanf("%d", & n); // copy the number m = n; //find the reverse while (n != 0) { // extract the unit digit r = n % 10; // store the reverse number // give appropriate positional value // of each digit revnum = revnum * 10 + r; //divide the number by 10 n = n / 10; } // check for palindrome if (m == revnum) printf("%d is a palindrome!", m); else printf("%d is not a palindrome!", m); return 0; }
Output
Case 1:
Enter the number = 123321
123321 is a palindrome!
Case 2:
Enter the number = 9
9 is a palindrome!
Case 3:
Enter the number = 1234
1234 is not a palindrome!
5. C Program & output to check if a number is a palindrome or not using recursion
/********************************** alphabetacoder.com C program to check if a number is a palindrome or not using recursion ***********************************/ #include <stdio.h> // recursive function to check if // a number is a palindrome or not int check_palindrome(int n, int num, int rev) { if (n == 0) { // reverse is same as original if (num == rev) return 1; else return 0; } else return check_palindrome(n / 10, num, (rev * 10 + n % 10)); } int main() { // declare variables int num; // take input of the number printf("Enter the number = "); scanf("%d", & num); // check for palindrome // by calling the function // If the function returns 1, // then the number is palindrome // else the number is not palindrome if (check_palindrome(num, num, 0)) printf("%d is a palindrome!", num); else printf("%d is not a palindrome!", num); return 0; }
Output
Case 1:
Enter the number = -212
-212 is a palindrome!
Case 2:
Enter the number = 9
9 is a palindrome!
Case 3:
Enter the number = 1334
1334 is not a palindrome!