Created
January 4, 2022 16:43
-
-
Save HaradaKumiko/1b1685808a3ed2d8eebb529d49b7d17b 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 FactorialRecursive { | |
public static void main(String[] args) { | |
Long angka; | |
Scanner inputan = new Scanner(System.in); | |
System.out.println("Masukan Angka : "); | |
angka = inputan.nextLong(); | |
System.out.println("Factorial dari " + angka + " adalah " + factorRecursive(angka)); | |
System.out.println("Factorial dari " + angka + " adalah " + factorialLoop(angka)); | |
} | |
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