Factorial

Run Settings
LanguageJava
Language Version
Run Command
class Factorial { public Factorial(){ } public int findFactorialRecursive(int number){ if (number < 0){ System.out.println("Cannot do a factorial of negative numbers."); return -1; } if (number ==2 || number == 1 || number == 0){ return number; } return number * findFactorialRecursive(number-1); } public int findFactorialIterative(int number){ if (number < 0){ System.out.println("Cannot do a factorial of negative numbers."); return -1; } int total =number; while (number > 1){ number--; total *=number; } return total; } } class Main { public static void main(String[] args) { Factorial factorial = new Factorial(); System.out.println("Recursive: "+factorial.findFactorialRecursive(5)); System.out.println("Iterative: "+factorial.findFactorialIterative(5)); } }
Editor Settings
Theme
Key bindings
Full width
Lines