adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to print reverse of a given number Using recursive function

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

Post a Comment

0 Comments