Membuat program menghitung faktorial dengan Java.
Jawab :
package DoubleMetHod;
import java.io.*;
public class FaktoriaL {
public static void main(String[] args) {
BufferedReader dataKata =new BufferedReader (new InputStreamReader (System.in));
DataInputStream dataAngka = new DataInputStream (System.in);
int n=0;
int total=1;
System.out.println("Masukkan n faktorial");
try{
n=Integer.parseInt(dataAngka.readLine());
}
catch(IOException e){System.out.println("Terjadi Error");}
System.out.println("Hasil faktorial "+faktorial(n));
}// bagian Akhir
public static int faktorial(int n){
int hasil=1;
int i=1;
while(i<=n){hasil=hasil*i;i++;
}
return hasil;}
}
Screenshoot :
Hasil :