Ive been working on a program for a couple hours now for my C class, and it's a question im sure most of you have seen before:
Write a function that takes an integer value and
returns the number with it's digits reversed. For example,
given the number 7631, the function should return 1367
The question isn't exactly what im having a problem with though, im not getting the answer im expecting (im positive the math is fine) rather, im getting a large negative number in its place
Code:
/*************************************************************
* File: asgnX.c *
* Course: cisp360 Folsom Lake College *
* Author: your name here *
* Date: mm/dd/yyyy format *
* Assignment Nr: X *
* Assignment Description: *
* 1. Write a function that takes an integer value and *
* returns the number with it's digits reversed. For example, *
* given the number 7631, the function should return 1367 *
* *
* 2. Edit so it is easy to read (for the instructor) and *
* doesn't wrap to the next line in a terminal screen *
* *
* 3. Limit the number of characters in your source file *
* to 78 characters or less per line *
*************************************************************/
#include<stdio.h>
#include<math.h>
int reversenum(int);
int main ()
{
int num1;
int result;
num1 = 0;
result = 0;
printf("Enter a number between 1 and 10,000 inclusive: ");
scanf("%d", &num1 );
if (num1 < 0 || num1 > 10000)
{
printf("Invalid Entry\n\n");
return -1;
}
result = reversenum(num1);
printf("The reversed number is: %d\n\n", &result);
return 0;
}
int reversenum(int num1)
{
int num2;
int num3;
int rev;
int place;
int placecnt;
num2 = 0;
num3 = 0;
rev = 0;
place = 0;
placecnt = 0;
place = floor(log10(num1));
num2 = num1;
for (placecnt = 0; placecnt <= place; placecnt++)
{
num3 = floor(num2/ pow(10, (place-placecnt)));
num2 = num2 - num3* pow(10, (place-placecnt));
rev = rev + num3* pow(10, placecnt);
}
return rev;
}
The output appears as such:
Enter a number between 1 and 10,000 inclusive: 1234
The reversed number is: -1075628736
im not entirely sure what it is that i am doing wrong, i have been trial and error-ing for a couple of hours now, and i would much appreciate any assistance