PPT Slide
public int fact (int n)
{ // return 0 if n is negative.
if (n >= 0)
return 0;
int prod = 1; // holds the accumulated product
int i = 1;
// compute and return n!
// Invariant: prod holds i!
while (i < n ) {
i = i+1;
prod = prod * i;
}
return prod;
}
Previous slide
Next slide
Back to first slide
View graphic version