Hello, my name is kerry and i just sign up .Well, my question is how do reverse a number input by user using recersive function.Thanks for the help.

Hello, my name is kerry and i just sign up .Well, my question is how do reverse a number input by user using recersive function.Thanks for the help.

You mean recursive funtion.The concept is quite simple.You take a number (to be reversed) and divide it by 10 and add the remainder to another number.As every loop goes by you multiply 10 to the other number and keep adding the remainders till you get 0.

Check this out

int n=12345,m=0;
cout<<"Orginal No:"<<n;
while(n>0)
{
    m *= 10;
    m += n%10;
    n /= 10;
}

cout<<"\nRevesed No:"<<m;

Hi,
Your problem can be solved in various way.This program is simplest(but layman process).Here i assumed that input number is in between integer range
(i.e.-32768 to 32767)

#include<stdio.h>
#incliude<conio.h>



void reverse(int num)
{
int i,div;


for(i=0;i<5;i++)
{
if(num>9)
{
printf("%d",div);
break;
}


div=num%10;
num*=/10;
printf("%d",div);
}


}


void main()
{


int num;
clrscr();


printf("\nEnter the number:");
scanf("%d",&num);


reverse(num);


getch();
}

N.B.
If you have any problem in C.Please send it to me with your id.I try my best to solve it.my id is rishi_works@yahoo.co.in

thanking you...... Rishiraj bayerd.

Edited 3 Years Ago by Nick Evan: Fixed formatting

reverse(int n)
{
long sum=0;int rem;
while(n>0)
{rem=n%10;
sum=sum*10+rem;
n=n/10;}
return sum;


main()

................................ hope u can do it from here.good luck.

Edited 3 Years Ago by Nick Evan: Fixed formatting

the recursive code (function call itself ) will be as follow

#include <iostream>
#include <cstdlib>
int reverse_num (int n,int m) ;
using namespace std;
int main()
{
int n;
int m=0;
cout<<"enter number to reverse :";
cin>>n;
cout<<reverse_num(n,0)<<endl; //calling the function
system("pause") ;
return 0;
}
int reverse_num(int n,int m)
{
if(n==0)
return m; //base (exit condition)
m*=10;
m+=n%10;
return reverse_num(n/10,m); //recursive call
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

#include<stdio.h>
#include<conio.h>
main()
{
int num, rem=0;
printf("\n Enter the number ");
scanf("%d", &num);
while(num>0)
{
rem = (rem * 10) + (num % 10);
num = num / 10;
}
printf("\n Reverse of the number is %d", rem);
getch();
}
Comments
4 years late, nobody cares about your unportable and poorly formatted code.
This article has been dead for over six months. Start a new discussion instead.