Write a code fragment that computes n! (i.e. factorial).
You may assume n > 0 and that count
and factorial
have been
declared as int
s.
Using while | count = 1; factorial = 1; while (count <= n) { factorial *= count; count++; } |
Using do-while | count = 1; factorial = 1; do { factorial *= count; count++; } while (count <= n); |
Using for | factorial = 1; for (count = 1; count <= n; count++) { factorial *= count; } |
Write a code fragment that computes base
raised to exp
power,
where exp
is an integer and base
is a double.
Assume result
has been declared to be a double
and i
has been declared to be an int
.
Assume exp
is non-negative.
Using while | result = 1; i = 0; while (i < exp) { result *= base; i++; } |
Using do-while | result = 1; i = 0; do { result *= base; i++; } while (i < exp); |
Using for | result = 1; for (i = 0; i < exp; i++) { result *= base; } |