reversing number
Feb 10, 2015 at 11:48pm UTC
im writing a program to reverse number however i am having trouble with it.
ex:
input: 123
output:321
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include<iostream>
using namespace std;
double rev_digit(int num);
int main()
{
int num;
cout << "input number " ;
cin >> num;
cout << num << endl;
return 0;
}
double rev_digit(int num)
{
int rev_num=0;
while (num > 0)
{
rev_num= rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}
Feb 11, 2015 at 12:22am UTC
Is there any reason you're not just computing it as a string?
Seems like you're making it unnecessarily difficult.
Feb 11, 2015 at 12:40am UTC
This algorithm will successfully reverse your entered integer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
using namespace std;
int main(){
int num,i=10;
cin>>num;
do {
cout<< (num%i)/ (i/10);
i *=10;
}while ((num*10)/i!=0);
system("pause" );
}
Feb 11, 2015 at 12:50am UTC
i need to use a function for my assignment
Feb 11, 2015 at 12:52am UTC
Then just put that code in a function.
Feb 11, 2015 at 1:21am UTC
Did anyone actually look at OPs code?
@axel609 your rev_digit function is correct except that the return type should be int. The problem is you do not call the function in main.
Topic archived. No new replies allowed.