Calculating faculties

This is a discussion on Calculating faculties within the C++ Programming forums, part of the General Programming Boards category; I know this has been asked 100 times before and I searched the posts but I didn't find the code ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Calculating faculties

    I know this has been asked 100 times before and I searched the posts but I didn't find the code so I'll ask. How do you write a code that calculates the faculty for n, i.e. n! ?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Code:
    #include <iostream>
    
    using std::cout;
    
    int factorial(int input);
    
    int main()
    {
    	cout << factorial(3) << "\n";
    	return 0;
    }
    
    int factorial(int input)
    {
    	for (int output=1; input > 0; input--)
    		output = output * input;
    	return output;
    }
    Last edited by MadCow257; 03-26-2005 at 11:20 AM.

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Recursive function
    Code:
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    int factorial(int fac)
    {
      if(fac == 1)
      {
        return fac;
       }
      else
      {
         return fac * factorial(fac - 1);
      }
    }
    
    int main()
    {
      int fac;
      cout << "INPUT: ";
      cin >> fac;
      cout << "FACTORIAL OF " << fac << " IS " << factorial(fac) << endl;
     system("PAUSE");
     return 0;
    }
    Last edited by mrafcho001; 03-26-2005 at 11:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  2. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  3. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  4. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM
  5. Calculating window sizes
    By Mox in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 08:17 PM

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