Explanation:
- We first take an integer as input from the user using
scanf
. - We then store the original number in a separate variable called
originalNum
. - We use a while loop to reverse the input number. In each iteration, we get the remainder of the number when divided by 10 using the modulus operator (%). We then add this remainder to the
reversedNum
variable after multiplying it by 10 (to shift the existing digits to the left by one place). Finally, we divide the original number by 10 to discard the last digit. - After the while loop ends, we check if the original number is equal to the reversed number. If they are equal, we print a message saying that the number is a palindrome. Otherwise, we print a message saying that the number is not a palindrome.
Enter an integer: 1232112321 is a palindrome number.
In this example, the user enters the number 12321. The program then checks whether this number is a palindrome or not. Since the number reads the same forward and backward, it is a palindrome, and the program prints a message to that effect.
C program to check whether a number is a palindrome or not using recursion
#include <stdio.h>
int isPalindrome(int num, int temp) { if(num == 0)
{
return temp; }
temp = (temp * 10) + (num % 10);
return isPalindrome(num/10, temp);
}
int main()
{
int num, rev;
printf("Enter a number: ");
scanf("%d", &num);
rev = isPalindrome(num, 0);
if(rev == num) {
printf("%d is a palindrome number.\n", num);
}
else {
printf("%d is not a palindrome number.\n", num);
}
return 0;
}
In this program, we first define a recursive function isPalindrome that takes two arguments num and temp. num represents the number we want to check, while temp is an intermediate variable used to store the reversed number. The function works by repeatedly dividing num by 10 to get its digits, and then multiplying temp by 10 and adding the digit to it. Once num becomes 0, the function returns the reversed number stored in temp.
In the main function, we ask the user to enter a number and call the isPalindrome function to check whether it is a palindrome or not. If the reversed number is equal to the original number, we print a message indicating that it is a palindrome. Otherwise, we print a message indicating that it is not a palindrome.
Sample output :
Enter a number: 12321
12321 is a palindrome number.
In this example, the user enters the number 12321. The program then calls the isPalindrome function to check whether it is a palindrome or not. Since the number is a palindrome, the function returns the reversed number (which is also 12321). The program then prints a message indicating that the number is a palindrome.
C program to write fibonacci series