Thread: Trouble with Factorial.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Clovis, California, United States
    Posts
    1

    Trouble with Factorial.

    I was given a question in my programming class to create a program to find the factorial of any number greater than zero and to use Gosper's formula to approximate it. I'm not sure what I am doing wrong so any help would be appreciated!

    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.14159265
    double equation(int n);
    int
    main(void)
    {
    int input, double answer;
    printf("Please input a number>");
    scanf("%d", &input);
    answer = equation(input);
    printf("%d equals approximately %.2f\n", input answer);
    return (0);
    }
    
    
    double equation(n)
    {
    double nfaca, nfacb, nfacc, nfacd;
    nfaca = pow(n,n) * pow(e,-n);
    nfacb = ((2 * n) + .33);
    nfacc = sqrt(nfacb * PI);
    nfacd = nfaca * nfacc;
    return (answer);
    }
    

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Pay attention to your compiler warnings.

    *You need a comma between input and answer in the printf.
    *If you're going to use the constant "e" in equation, you need to define it somewhere.
    *If you're going to return "answer", you need to declare it somewhere. I think you mean to return (nfact).
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int input, double answer;
    Does that compile?
    Code:
    int input;
    double answer;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. factorial value
    By joybanerjee39 in forum C Programming
    Replies: 4
    Last Post: 11-14-2011, 12:40 PM
  2. Factorial
    By SpockRox08 in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2010, 02:07 AM
  3. Factorial Help
    By purplehaze in forum C Programming
    Replies: 6
    Last Post: 04-06-2010, 11:19 PM
  4. Factorial 100!
    By ganesh bala in forum C Programming
    Replies: 5
    Last Post: 06-24-2009, 01:34 AM
  5. Factorial
    By DonW in forum C Programming
    Replies: 10
    Last Post: 09-19-2004, 04:53 PM