Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. // factorialr.cpp
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. // Function for calculating factorial:
  7. unsigned long long factorial(unsigned short int n) {
  8.   // Define variables:
  9.   long long f; // For calculating result
  10.   unsigned short int i; // Iterational value
  11.   // Initialise:
  12.   if (n > 0) f = n; // If calculating factorial of n >= 1 result should be n * (n - 1) ... * 2 * 1;
  13.   else f = 1;   // If calculating factorial of 0 result should be 1:
  14.   i = n;
  15.   // Calculation:
  16.   while (i-- > 1) { f = f * (i); }
  17.   return f; // Return result
  18. }
  19.  
  20. int main()
  21. {
  22.   unsigned short int n;
  23.   cout << "Find factorial of (enter positive integer): ";
  24.   cin >> n;  
  25.   cout << "Answer: " << n << "! = " << factorial(n) << endl;
  26.   return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement