C program to check if a number is palindrome using recursion
Discover how to write a C program to check if a number is a palindrome using recursion. This article provides an explanation of palindromes, a recursive algorithm, and complete code examples.
A palindrome is a number (or a word, phrase, or sequence) that reads the same backward as forward. For example, the numbers 121, 1331, and 12321 are palindromes. In this article, we will focus on writing a C program to check if a given number is a palindrome using recursion. We will delve into the concept of recursion, outline the algorithm, and provide a complete C program with thorough explanations.
Understanding Palindromes and Recursion
To determine if a number is a palindrome, we need to check if the number reads the same from left to right as it does from right to left. Recursion, a technique where a function calls itself, can be particularly useful for such problems as it allows us to break down the problem into smaller, more manageable parts.
Algorithm to Check Palindrome Using Recursion
- Base Case:
- If the number has a single digit or no digits (when divided to 0), it is a palindrome.
- Recursive Case:
- Compare the first and last digits of the number.
- If they are the same, remove these digits and check the remaining part of the number recursively.
Extracting Digits Using Recursion
To check if a number is a palindrome using recursion, we need a way to compare the first and last digits. Here’s the approach:
- Determine the Number of Digits:
- Use a helper function to calculate the number of digits.
- Extract First and Last Digits:
- The last digit can be obtained using the modulus operator
% 10
. - The first digit can be determined using division by the appropriate power of 10.
- The last digit can be obtained using the modulus operator
- Recursive Reduction:
- Remove the first and last digits and check the remaining number.
Write a C program to check if a number is palindrome using recursion
Below is a C program to check if a number is a palindrome using recursion:
#include <stdio.h>
#include <math.h>
// Recursive function to check if a number is palindrome
int isPalindrome(int num, int temp) {
// Base case: If the number becomes 0
if (num == 0)
return temp;
// Recursive case
temp = temp * 10 + num % 10; // Reverse the number
return isPalindrome(num / 10, temp);
}
int main() {
int number, reverse = 0;
// Input the number from the user
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is a palindrome
if (number == isPalindrome(number, reverse))
printf("%d is a palindrome.", number);
else
printf("%d is not a palindrome.", number);
return 0;
}
Output
Enter a number: 12321
12321 is a palindrome.
Explanation of the Code
- Recursive Function
isPalindrome
:- The function
isPalindrome
takes two arguments:num
(the number to check) andtemp
(a temporary variable to store the reversed number). - Base Case: If
num
is 0, the function returnstemp
, which contains the reversed number. - Recursive Case:
temp = temp * 10 + num % 10;
updatestemp
with the last digit ofnum
.return isPalindrome(num / 10, temp);
recursively callsisPalindrome
with the remaining part ofnum
.
- The function
- Main Function
main
:- The user is prompted to enter a number.
- The function
isPalindrome
is called with the initial number andreverse
set to 0. - If the returned value matches the original number, it is a palindrome; otherwise, it is not.
Example Runs
Example 1
Enter a number: 1221
1221 is a palindrome.
Example 2
Enter a number: 12345
12345 is not a palindrome.