integer reversed function

I'm a newbee to c++. I need help with a reversed function this is as far as I can get.Thank's all.

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
39
40
41
42
43
44
45
#include <iostream>
using namespace std;

void reverse(int numb);

int main()
{
//Variables
	int numb;
	int num;
	int digit1;
	int digit10;
	int digit100;
	int digit1000;
	

	//Input a 4 digit number
	cout << "This program will reverse any 4 digit number entered by the user. \n";
	cout << "Enter the 4 digit number to be reversed. \n";
	cin  >> num;

	
void reverse(int num);
{
	
	//This code will separate the four digit number one by one.
	 
	digit1=	num % 10;
	 num=		num / 10;
	 digit10=	num % 10;
	 num=		num / 10;
	 digit100=	num %10;
	 num=		num / 10;
	 digit1000=	num;
	 num=		numb;
	
	//Reverse the number
	int numb=(digit1*1000)+(digit10*100)+(digit100*10)
		+(digit1000);
}
	//Print out the reversed number
	cout << "Your number reversed is = " << numb << "\n\n";

	return 0;
}
Looks like this would work; is there something you were having trouble with?
This code is not reversing the number at all. Any ideas why is not working?
am I calling the reversed function in the proper way and with the right parameters? Please help.
I changed you code and it works fine...
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// reverse0317.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

void reverse(int numb);

int _tmain(int argc, _TCHAR* argv[])
{
	//Variables
	int num;

	//Input a 4 digit number
	cout << "This program will reverse any 4 digit number entered by the user. \n";
	cout << "Enter the 4 digit number to be reversed. \n";
	cin  >> num;

	reverse(num);//call the function here, and move the function body out of main function 

	return 0;
}


void reverse(int num0)
{
	//Variables
	int numb;
	int digit1;
	int digit10;
	int digit100;
	int digit1000;
    int num = num0;
	//This code will separate the four digit number one by one.

	digit1=	num % 10;
	num=		num / 10;
	digit10=	num % 10;
	num=		num / 10;
	digit100=	num %10;
	num=		num / 10;
	digit1000=	num;
	//num=		numb; seems this is a useless line

	//Reverse the number
	numb=(digit1*1000)+(digit10*100)+(digit100*10)
		+(digit1000);

	//Print out the reversed number
	cout << "Your number reversed is = " << numb << "\n\n";
}

but I'd like to suggest you a function like this
1
2
3
4
5
6
7
8
9
10
11
12
13
//reverse numbers of any digit
int reverse_num(int sourcenum)
{
	int temp = sourcenum;
	int sum = 0;
	while (temp)
	{
		sum*=10;
		sum += temp%10;
		temp/=10;
	}
	return sum;
};
Last edited on
Topic archived. No new replies allowed.