-
-
Save novoselrok/f12a9c09038260b4ea37 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
package numbers; | |
import java.util.Scanner; | |
public class Factorial { | |
private static int factorial(int num){ | |
if(num == 1 || num == 0) return 1; | |
else return num * factorial(num - 1); | |
} | |
public static void main(String[] args) { | |
Scanner scannerObject = new Scanner(System.in); | |
int factorialNum = scannerObject.nextInt(); | |
//ITERATION | |
int sum = 1; | |
for(int i = 1; i <= factorialNum; i++){ | |
sum = sum * i; | |
} | |
System.out.println("Iteration: " + sum); | |
//RECURSION | |
System.out.println("Recursion: " + factorial(factorialNum)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment