Program in C and C++ to check a given number is palindrome or not Using recursive function
C Program
#include<stdio.h> #include<math.h> int digits(int n){ int d = 0; while(n != 0){ d++; n /= 10; } return d; } int reverse(int n){ if(n == 0) return 0; return ((n % 10) * pow(10, digits(n) - 1)) + reverse(n / 10); } int main(){ int n; printf("Enter a number :- "); scanf("%d",&n); if( reverse(n) == n) printf("%d is palindrome number.",n); else printf("%d is not palindrome number.",n); return 0; }
C++ Program
#include<iostream> #include<math.h> using namespace std; int digits(int n){ int d = 0; while(n != 0){ d++; n /= 10; } return d; } int reverse(int n){ if(n == 0) return 0; return ((n % 10) * pow(10, digits(n) - 1)) + reverse(n / 10); } int main(){ int n; cout<<"Enter a number :- "; cin>>n; if( reverse(n) == n) cout<<n<<" is palindrome number."; else cout<<n<<" is not palindrome number."; return 0; }
Output :-
Related Programs
Coming soon :
0 Comments