Pages

Saturday, 24 December 2016

Problem 51 - Faktorial Finder

      Find factorial of a given number using recursion adalah program mencari faktorial dari input yang di berikan

Code di bawah ini :
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. import java.util.Scanner;
  2. class FactorialDemo{
  3.    public static void main(String args[]){
  4.       Scanner scanner = new Scanner(System.in);
  5.       System.out.println("Angka :");
  6.      
  7.       int num = scanner.nextInt();
  8.      
  9.       int factorial = fact(num);
  10.       System.out.println("Faktorial dari " +num+ ": "+factorial);
  11.    }
  12.    static int fact(int n)
  13.    {
  14.        int output;
  15.        if(n==1){
  16.          return 1;
  17.        }
  18.        
  19.        output = fact(n-1)* n;
  20.        return output;
  21.    }
  22. }


Disini saya input 5 maka 5*4*3*2*1 = 120

0 comments:

Post a Comment