Program in C and C++ to print reverse of a given number 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); printf("Reverse : %d",reverse(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; cout<<"Reverse : "<<reverse(n); return 0; }
Output :-
Related Programs
sfsd
0 Comments