Advertisement
satriafu5710

Menghitung Bilangan Faktorial dengan Cara Iteratif & Rekursif C++

Oct 29th, 2022
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int faktorial_iteratif(int n)
  5. {
  6.  
  7.     int hasil = 1;
  8.  
  9.     for (int i = 1; i <= n; i++)
  10.     {
  11.  
  12.         hasil = hasil * i;
  13.     }
  14.  
  15.     return hasil;
  16. }
  17.  
  18. int faktorial_rekursi(int a)
  19. {
  20.  
  21.     if (a <= 1)
  22.     {
  23.  
  24.         return 1;
  25.     }
  26.  
  27.     else
  28.     {
  29.  
  30.         return a * faktorial_rekursi(a - 1);
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.  
  37.     int iteratif, rekursi;
  38.  
  39.     cout << "\n\t Hitung Faktorial dengan Iteratif & Rekursi \n";
  40.  
  41.     cout << "\n Nilai iteratif : ";
  42.     cin >> iteratif;
  43.  
  44.     cout << "\n Nilai rekursi  : ";
  45.     cin >> rekursi;
  46.  
  47.     cout << "\n Hasil faktorial iteratif : " << faktorial_iteratif(iteratif);
  48.  
  49.     cout << "\n Hasil faktorial rekursi  : " << faktorial_rekursi(rekursi);
  50.  
  51.     cout << endl
  52.          << endl;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement