•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,465 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,240 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 16713 | Replies: 5
![]() |
•
•
Join Date: Jul 2004
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
U Means how to make a factorial program then mail me at saadkhulsai@yahoo.com
•
•
•
•
Originally Posted by Kerry
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;•
•
Join Date: Jul 2004
Posts: 16
Reputation:
Rep Power: 5
Solved Threads: 0
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.
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.
•
•
Join Date: Aug 2004
Posts: 11
Reputation:
Rep Power: 5
Solved Threads: 0
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
}
#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
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- add even (or odd) numbers from input (C++)
- Limiting numbers displayed, under a user given value (Java)
- Need Help With Error Checking User Input (C)
Other Threads in the C Forum
- Previous Thread: Linking "park" ERROR
- Next Thread: passing arrays,reference and value parameters


Linear Mode