Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // factorialr.cpp
- #include <iostream>
- using namespace std;
- // Function for calculating factorial:
- unsigned long long factorial(unsigned short int n) {
- // Define variables:
- long long f; // For calculating result
- unsigned short int i; // Iterational value
- // Initialise:
- if (n > 0) f = n; // If calculating factorial of n >= 1 result should be n * (n - 1) ... * 2 * 1;
- else f = 1; // If calculating factorial of 0 result should be 1:
- i = n;
- // Calculation:
- while (i-- > 1) { f = f * (i); }
- return f; // Return result
- }
- int main()
- {
- unsigned short int n;
- cout << "Find factorial of (enter positive integer): ";
- cin >> n;
- cout << "Answer: " << n << "! = " << factorial(n) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement