I found multiple iterative solutions in the net for defining the factorial of n. They look something like this:
int Factorial(int number)
{
int factorial = 1;
for (int i = 1; i <= number; i++)
factorial *= i;
return factorial;
}
Doesn’t Factorial(0) = 1 and Factorial(1) = 1? Therefore, the counter variable inside the for loop should start with 2 since everything below it will yield 1.
for (int i = 2; i <= number; i++)
factorial *= i;
Is there some reason why they used 1 as the starting number for the counter?
It doesn’t matter – either 1 or 2 will work, as multiplying by 1 does nothing. However, most loops start with 0 or 1, and this just follows the pattern. Also, the definition of factorial is often stated as the product of all positive integers up to n, so this includes one. Essentially, 1 is, aesthetically, a better starting point.