Recursion/Iteration - Factorial

Run Settings
LanguageC#
Language Version
Run Command
using System; class MainClass { static void Main() { // Factorial - is going down from the number multiplying each till reach 1 // e.g. of 5 or 5! = 5 * 4 * 3 * 2 * 1 = 120 // Functions that find the factorial of any number Console.WriteLine($"Iterative: {FindFactorialIterative(5)}"); Console.WriteLine($"Recursive: {FindFactorialRecursive(5)}"); } static int FindFactorialRecursive(int number) //O(n) { if (number == 0 || number == 1) { return 1; } if (number == 2) { return 2; } return number * FindFactorialRecursive(number - 1); } static int FindFactorialIterative(int number) //O(n) { int answer = 1; if (number == 0 || number == 1) { return 1; } // 0x1 if (number == 2) { return 2; } // 1x2 for (int i = 2; i <= number; i++) { answer *= i; } return answer; } } // Iterative // answer is 1, then 2, then 6, then 24 // i is 2, then 3 then 4 then 5 // (1xi2) (2xi3) (6xi4) (24xi5) // Recursive // The function called inside of the function is seperate with ITS OWN parameter each time
Editor Settings
Theme
Key bindings
Full width
Lines