Strange Output

This is a discussion on Strange Output within the C Programming forums, part of the General Programming Boards category; Hello world, I'm studying functions and recursions. I'm trying to write functions that evaluates the power and the factorial - ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Strange Output

    Hello world,

    I'm studying functions and recursions. I'm trying to write functions that evaluates the power and the factorial - the very common exercise -. But, some strange output appears..
    Here's the code:
    Code:
    #include <stdio.h>
    
    int absolute(int num)
    {
        if(num > 0)
            return num;
        else
            return -num;
    }
    
    int loop_factorial(int num)
    {
        long int f = 1;
        while(num > 1)
            f *= num--;
        return f;
    }
    
    int recursive_factorial(int num)
    {
        if(num == 1)
            return num;
        else if(num <= 0)
            return 1;
        else
            return num * recursive_factorial(num - 1);
    }
    
    float loop_power(int num, int pow)
    {
        if(num == 0)
            return 0;
        if(pow == 0)
            return 1;
        int counter = 1;
        while(counter++ < absolute(pow))
            num *= num;
        if(pow>0)
            return num;
        else
            return 1/num;
    }
    
    int main(void)
    {
        char choice;
        int num, opt;
        while(1)
        {
            printf("Enter function: ");
            choice = getchar();
            if(choice == 'f')
            {
                choice = getchar();
                if(choice == 'r')
                {
                    printf("Enter: ");
                    scanf("%d", &num);
                    printf("%d! = %d\r\n", num, recursive_factorial(num));
                }
                else if(choice == 'l')
                {
                    printf("Enter: ");
                    scanf("%d", &num);
                    printf("%d! = %d\r\n", num, loop_factorial(num));
                }
            }
            else if(choice == 'p')
            {
                printf("Enter: ");
                scanf("%d %d", &num, &opt);
                printf("%d^%d = %f\r\n", num, opt, loop_power(num, opt));
            }
        }
        return 0;
    }
    And here's what I have tried:
    http://img808.imageshack.us/img808/4531/errornzh.png

    Uploaded with ImageShack.us

    Notes:
    - I have shown that the factorial ( both versions ) are working fine.
    - Why 2 of "Enter function: "?
    - why when using big numbers functions crash? ( I think that has something to do with the type of the return, but I have changed the type to long double but that fixed nothing )

    Any help?
    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to compute with long doubles, not just try to convert at the end.

    Also, "enter-key" is a character, so getchar() reads the enter-key, tries to process it, and can't, so your loop prints the prompt again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange Output
    By awr7126 in forum C Programming
    Replies: 8
    Last Post: 03-24-2011, 10:05 PM
  2. Strange Output
    By ThLstN in forum C Programming
    Replies: 2
    Last Post: 04-05-2008, 12:27 PM
  3. Strange output..
    By Tommy7 in forum C++ Programming
    Replies: 6
    Last Post: 08-23-2005, 04:09 AM
  4. output bit strange
    By onthewaytoC in forum C Programming
    Replies: 5
    Last Post: 06-10-2005, 05:36 AM
  5. strange output
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2003, 11:09 AM

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