Advertisement
juliarnasution

CI Rekursif

Apr 2nd, 2020
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @Author: juliarnasution
  5.  * @Date:   2020-04-03 10:07:14
  6.  * @Last Modified by:   Dell
  7.  * @Last Modified time: 2020-04-03 10:26:27
  8.  */
  9. defined('BASEPATH') OR exit('No direct script access allowed');
  10.  
  11. class Rekursif extends CI_Controller {
  12.  
  13.     public function index()
  14.     {
  15.         $this->contoh_rekursif(1);
  16.         echo "<br/>";
  17.         $n = 5;
  18.         echo "nilai faktorial ".$n." adalah ";
  19.         echo $this->faktorial($n);
  20.     }
  21.  
  22.     public function contoh_rekursif($param)
  23.     {
  24.         if ($param>10) {
  25.             return false;
  26.         }
  27.         echo "sekarang urutan ke ".$param;
  28.         echo "<br/>";
  29.         return $this->contoh_rekursif($param+1);
  30.     }
  31.  
  32.     public function faktorial($n)
  33.     {
  34.         if ($n>2) {
  35.             echo $n." * ";
  36.             return $n * $this->faktorial($n-1);
  37.         }else{
  38.             echo $n." * ".($n-1). "= ";
  39.             return $n;
  40.         }
  41.     }
  42.  
  43. }
  44.  
  45. /* End of file Rekursif.php */
  46. /* Location: ./application/controllers/Rekursif.php */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement