The factorial of a number is defined is the product of natural numbers from one to that particular number. Mathematically,
n! = 1 * 2 * 3 * …. * (n-1) * n
For example, the factorial of 5 is
5! = 1 * 2 * 3 * 4 *5= 120
This article will explain you how to find the factorial of a number through iteration as well as recursion.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static int Faktor(int num) { int result = 1; for(int i=1;i<=num;i++) { result *= i; } return result; } static void Main(string[] args) { Console.Write("Number : "); int number = Convert.ToInt32(Console.ReadLine()); int fact = Faktor(number); Console.WriteLine("{0} factorial is {1}", number, fact); Console.ReadKey(); } |
Output: