Skip to content

Instantly share code, notes, and snippets.

@HaradaKumiko
Created January 4, 2022 18:36
Show Gist options
  • Save HaradaKumiko/176d9f6db36d2564354efa6e5af46ddf to your computer and use it in GitHub Desktop.
Save HaradaKumiko/176d9f6db36d2564354efa6e5af46ddf to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class HitungFactorial {
public static void main(String[] args) {
boolean repeat = true;
Scanner input = new Scanner(System.in);
Scanner jawaban = new Scanner(System.in);
Long angka;
String jawab;
while (repeat){
System.out.println("Masukkan angka : ");
angka = input.nextLong();
System.out.println("Factorial dari " + angka + " adalah " + factorialLoop(angka));
//System.out.println("Factorial dari " + angka + " adalah " + factorRecursive(angka));
System.out.println("Apakah anda ingin mengulang? Y/N : ");
jawab = jawaban.nextLine();
if(jawab.equalsIgnoreCase("N")){
repeat = false;
}
}
}
static Long factorRecursive(Long angka){
if(angka == 1L){
return angka;
}else {
return angka * factorRecursive(angka-1L);
}
}
static Long factorialLoop(Long angka){
var result = 1L;
for (var counter = 1L; counter <= angka; counter++){
result *= counter;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment