rayhanf

rekursi vs looping

May 5th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.61 KB | Source Code | 0 0
  1. program rekursi_vs_looping;
  2. uses crt;
  3.  
  4. // fungsi rekursi
  5. function faktorial_rec(n: integer): integer;
  6. begin
  7.     if n = 0 then
  8.         faktorial_rec := 1
  9.     else
  10.         faktorial_rec := n * faktorial_rec(n-1);
  11. end;
  12.  
  13. // fungsi looping
  14. function faktorial_loop(n: integer): integer;
  15. var i, hasil: integer;
  16. begin
  17.     hasil := 1;
  18.     for i := 1 to n do
  19.         hasil := hasil * i;
  20.     faktorial_loop := hasil;
  21. end;
  22.  
  23. var angka: integer;
  24. begin
  25.     clrscr;
  26.     write('Masukkan angka: '); readln(angka);
  27.  
  28.     writeln('Faktorial menggunakan rekursi: ', faktorial_rec(angka));
  29.     writeln('Faktorial menggunakan looping: ', faktorial_loop(angka));
  30. end.
Add Comment
Please, Sign In to add comment