Calculate Factorial Iteratively

A common task is computing the factorial of a number. This iterative approach keeps the code minimal.

int n = 5;
int factorial = 1;
for (int i = 1; i <= n; i++) factorial *= i;
Console.WriteLine(factorial);


Explanation: A simple for-loop multiplies numbers from 1 to n to compute the factorial, all within just three lines of code.