Created
January 4, 2022 18:36
-
-
Save HaradaKumiko/176d9f6db36d2564354efa6e5af46ddf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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