Advertisement
Cnvmendoza

Factorial dowhile

Oct 28th, 2021 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main(){
  5.     // factorial starts at 1
  6.     // number = 1 is needed to store term, to multiply the term after another term has been created
  7.     // store stores input to store 1 without affecting input for if
  8.     // final multiplies input with number
  9.     double input, factorial = 1, term, store, number = 1, final;
  10.     cout << "Enter a Number: ";
  11.     cin >> input;
  12.     cout << input << "!" << " is ";
  13.     store = input;
  14.     if (input==0){
  15.         store = 1;
  16.     }
  17.     else if (input!=0){
  18.         do{
  19.             if (factorial<=store - 1){
  20.                 //(n - 1) ... (n - (store - 1)
  21.                 // stops when factorial <= store - 1
  22.                 term = input - factorial;
  23.             }
  24.             //(n - 1)(n - 2) ... stops when factorial <= store - 1
  25.             number *= term;
  26.             factorial++;
  27.         }while (factorial<=store - 1);
  28.     }
  29.     //n(n - 1)(n - 2) ...
  30.     final = store * number;
  31.     cout << setprecision(0) << fixed << final;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement