Program to check if given number is palindrome using recursion


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Write a program to check given number is palindrome or not using recursion
//Eg: 11211 is Palindrome ; 9839 is not Palindrome

 
#include <stdio.h>
int revnum(int );
int sum=0;
int main()
{
    int inp;
    printf("Enter a number:");
    scanf("%d",&inp);
    if(inp<0)
        printf("Invalid input...\n");
    else if(inp<10)
        printf("Given number is palindrome\n");
    else{
        if(inp==revnum(inp))
            printf("Given number is palindrome\n");
        else
            printf("Given number is not a palindrome\n");
}
    return 0;
}
int revnum(int n)
{
    if(n==0)
        return sum;
 else{
        sum=(sum*10)+(n%10);
        return revnum(n/10);
    }
}

No comments:

Post a Comment