Question: Program returning large negative value as output

This is a discussion on Question: Program returning large negative value as output within the C Programming forums, part of the General Programming Boards category; Ive been working on a program for a couple hours now for my C class, and it's a question im ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Question: Program returning large negative value as output

    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

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What is 12345 / 10 ? and 12345 % 10? Your solution takes on unnecessary complication. So, look at the results of div & mod and you'll see the majority of the solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 31
    Last Post: 08-16-2011, 08:32 AM
  2. Reading the program/output question
    By benrogers in forum C Programming
    Replies: 4
    Last Post: 01-26-2011, 03:21 PM
  3. Replies: 6
    Last Post: 06-05-2009, 09:42 AM
  4. Question: Output of data.txt from the following program
    By outlaw525 in forum C Programming
    Replies: 9
    Last Post: 06-23-2008, 03:33 PM
  5. Can we input negative numbers in the output windiw
    By hitesh1511 in forum C Programming
    Replies: 1
    Last Post: 08-22-2006, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21