Anonymous user
Factorial: Difference between revisions
Added Java, sample output doesn't need to be tabbed since it's labelled
(added Fortran) |
(Added Java, sample output doesn't need to be tabbed since it's labelled) |
||
Line 2:
Write a function to return the factorial of a number. Solutions can be iterative or recursive (though recursive solutions are generally considered too slow and are mostly used as an exercise in recursion). Support for trapping negative n errors is optional.
=={{header|Ada}}==
Line 93 ⟶ 90:
nfactorial = PRODUCT((/(i, i=1,n)/))
=={{header|Java}}==
===Iterartive===
<java>public static long fact(int n){
if(n < 0){
System.err.println("No negative numbers");
return 0;
}
long ans = 1;
for(int i = 1;i < n;i++){
ans *= i;
}
return ans;
}</java>
===Recursive===
<java>public static long fact(int n){
if(n < 0){
System.err.println("No negative numbers");
return 0;
}
if(n == 0) return 1;
return n * fact(n-1);
}</java>
=={{header|Python}}==
===Iterative===
Line 112 ⟶ 130:
</python>
===Numerical Approximation===
|