Advertisement
satriafu5710

Faktorial secara Rekursi Java

Dec 7th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. package com;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner userInput = new Scanner(System.in);
  9.         int angka, hasil;
  10.  
  11.         System.out.println("\t Nilai Faktorial");
  12.         System.out.print("\n Masukkan Bilangan : ");
  13.         angka = userInput.nextInt();
  14.  
  15.         System.out.println(); // untuk spasi
  16.  
  17.         hasil = faktorial(angka);
  18.         System.out.println("\n Nilai Faktorialnya : " + hasil);
  19.     }
  20.  
  21.     public static int faktorial(int n) {
  22.         if (n <= 1) {
  23.             System.out.println(n);
  24.             return n;
  25.         } else {
  26.             System.out.print("" + n + " x ");
  27.             return n * faktorial(n - 1);
  28.         }
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement