Reverse Of A Number.

write a program which accepts a number and displays the reverse of the number.
ex. input: 789
output: 987
It will be something like this. This program takes 3 numbers and displays them in increasing order. Use this format for your program. Let me know if you have questions.

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
34
35
36
37
38
#include <iostream>

using namespace std;

int main(){

  // inputs the numbers
  cout << "Enter three numbers: ";
  int one, two, three;
  cin >> one >> two >> three;

  int tmp;

  // orders one and two
  if (one > two){
    tmp = one;
    one = two;
    two = tmp;
  }

  // orders two and three
  if (two > three){
    tmp = two;
    two = three;
    three = tmp;
  }

  // orders one and two
  if (one > two){
	  tmp = one;
	  one = two;
	  two = tmp;
  }

  // outputs the sorted numbers
  cout << "The sorted numbers are: ";
  cout << one << " " << two << " " << three << endl;
}

http://www.cplusplus.com/forum/beginner/812/
int reverse_num(int sourcenum)
{
int temp = sourcenum;
int sum = 0;
while (temp)
{
sum*=10;
sum += temp%10;
temp/=10;
}
return sum;
};
or use c++ homework help
http://www.cpp4u.com/cpp_homework_help.html
Last edited on
Can you use strings, because if you can - then string::reverse_iterator would work for you perfect.

1
2
3
4
5
6
string num;
cout << "Enter num";
getline(cin, num);

for (string::reverse_iterator rit=num.rbegin; rit!=num.rend; ++rit){
cout << *rit;
Last edited on
Topic archived. No new replies allowed.