Thread: Factorials-more

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    Factorials-more

    Help please. It won't output the right numbers.

    Code:
    #include <iostream.h>
    int main()
        {
        float a,b,c,d,e,f,g;
        a,b,c,d,e,f,g = 1;
        float factorial;
        cout << "Push a number that you would like to factorialize." << endl;
        cin >> factorial;
        cout << "You chose " << factorial << endl;
        float factwhile;
        factwhile = factorial;
        while(factwhile > 0)
                        {
                        a = factorial - 1;
                        factwhile = factwhile - 1;
                        b = factorial - 2;
                        factwhile = factwhile - 1;
                        c = factorial - 3;
                        factwhile = factwhile - 1;
                        d = factorial - 4;
                        factwhile = factwhile - 1;
                        e = factorial - 5;
                        factwhile = factwhile - 1;
                        f = factorial - 6;
                        factwhile = factwhile - 1;
                        g = factorial - 7;
                        factwhile = factwhile - 1;
                        }
        float answer;
        answer = a*b*c*d*e*f*g;
        cout << answer << endl;
        cin.get();
        return 0;
        }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    Im not sure if that is how you have to write your program for a class or whatever but I wrote this factorial function along with a combination function and permutation but the combination one is buggy yet.

    Code:
    int factl (int n)
    {
      int product = 1;
      while (n>=1)
        {
         
       
              product *= n;
                n -= 1;
     
        }
    thats the factorial one seems to work fine for me.

    here is the permutation (it uses the above function):

    Code:
    int nPr (int n, int r)
    
    {
      long double nPr = factl(n)/factl(n-r);
        }
    i dont know if that helped at all, i never really thought anyone would be seeing it so its not commented but any questions just ask, ill check back frequently.

    edit: you can only do up to 12! because thats all it can handle but since i cant get my if statements working in these functions right now I cant fix it to not accept a value > 12.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    A few errors:
    Code:
    int factl (int n)
    {
      int product = 1;
      while (n>=1)
      {
        product *= n;
        n -= 1;
      }
    return product;
    }
    Code:
    long nPr (int n, int r)
    {
      return factl(n)/factl(n-r);
    }
    And for choosing, its

    Code:
    long nCr (int n, int r)
    {
        return factl(n)/(factl(n-r) * factl(r));
        return nCr;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need suggestion on sum of factorials.
    By chhay in forum C Programming
    Replies: 27
    Last Post: 08-01-2008, 05:13 AM
  2. Problem with Recursion calculing Factorials
    By cvelasquez in forum C Programming
    Replies: 7
    Last Post: 12-14-2005, 08:23 AM
  3. Function To Find Factorials!!! Please Help
    By adzza69 in forum C++ Programming
    Replies: 22
    Last Post: 08-20-2004, 06:39 PM
  4. Program to calculate factorials
    By forzamilan in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2001, 10:24 AM