I am having problems writing a code in java to compute n! without recursion. I know how to do it in loops, but I am not sure how to do it non-recursively.
procedure factorial
if n = 1 or n = 0
return 1
if n>1
return(n*factorial(n-1))
end
I am having problems writing a code in java to compute n! without recursion. I know how to do it in loops, but I am not sure how to do it non-recursively.
procedure factorial
if n = 1 or n = 0
return 1
if n>1
return(n*factorial(n-1))
end
Here is an iterative solution:
int factorial(int n) {
int f = 1;
for(int i=1;i<=n;i++) {
f *= i;
}
return f;
}