reversing number

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;
}
Is there any reason you're not just computing it as a string?

Seems like you're making it unnecessarily difficult.
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");
}

i need to use a function for my assignment
Then just put that code in a function.
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.