#include <iostream>
usingnamespace 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;
}
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.
// reverse0317.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
usingnamespace 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;
};