Skip to content

Instantly share code, notes, and snippets.

@erikgunawan
Created October 20, 2014 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikgunawan/c15088b545ffad54bdb2 to your computer and use it in GitHub Desktop.
Save erikgunawan/c15088b545ffad54bdb2 to your computer and use it in GitHub Desktop.
Menghitung Nilai Faktorial Bilangan dengan Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @Developer : Erik Gunawan
* @Nickname : EXz
* @Phone : 0896 5332 0577
* @EMail : admin@erikgunawan.com
* @Gmail : erik.gunawan44@gmail.com
* @Website : www.erikgunawan.com
* @Facebook : facebook.com/revolutionist.exz
* @Twitter : twitter.com/_EXz
*/
public class Faktorial {
private static BufferedReader br = null;
public static void main(String[] args) {
System.out.println("===========================================");
System.out.println("Program Menghitung Nilai Faktorial Bilangan");
System.out.println("===========================================");
System.out.println("");
boolean cek = true;
while (cek) {
br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Masukkan bilangan asli : ");
int bil = Integer.parseInt(br.readLine());
System.out.println("");
String output = "Nilai faktorial dari " + bil + "! adalah ";
if (bil==0)
System.out.println(output + "1");
else if (bil>0) {
int hasil = 1;
for (int i=bil; i>0; i--) {
hasil *= i;
}
System.out.println(output + hasil);
}
else
System.out.println("Nilai yang Anda masukkan bukan bilangan asli!");
System.out.print("Ingin coba lagi (Y/N) ? ");
String coba = br.readLine();
if (coba.equalsIgnoreCase("N"))
cek = false;
else if (coba.equalsIgnoreCase("Y"))
cek = true;
else
System.exit(0);
}
catch (IOException ioe) {
System.out.println("Error IOException");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment