Advertisement
skb50bd

Factorial (Iterative and Recursive)

Mar 29th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int recursiveFact(int n) {
  5.     return n ? n * recursiveFact(n - 1) : 1;
  6. }
  7.  
  8. int iterFact(int n) {
  9.     int res = 1;
  10.     while (n) {
  11.         res *= n;
  12.         n--;
  13.     }
  14.     return res;
  15. }
  16.  
  17. int main() {
  18.     int n;
  19.     cin >> n;
  20.  
  21.     cout << "Recursive Factorial: " << recursiveFact(n) << endl;
  22.     cout << "Iterative Factorial: " << iterFact(n) << endl;
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement