Wednesday, November 9, 2011


package koderekursi;
import java.util.Scanner;


public class rekursi{
public static long faktorial (int n){
if ((n!=1)&& (n!=0))
return (n*faktorial(n-1));
else
{
return (1);
}
}

public static void main(String[] args) {
int n;
Scanner input=new Scanner (System.in);
System.out.print("masukkan bilangannya :");
n=input.nextInt();
System.out.print("hasil faktorial :"+faktorial (n)+"\n");
}

}


in the code below, we'll make a factorial method in java using "rekursi" i don't how to say it in english hehehe :P
we can see on the class below, that it used method to call the value of n,
method if.... return means that the value of n, will be multiplied as long as the value of n not 0 or 1, when it reach 1, then the loop will stop.

No comments:

Post a Comment

fill it with what you want, but please not be anarky and vulgar....