while
| Code | Execution |
|---|---|
|
Enter repetitions: 3 Do not copy! Do not copy! Do not copy! |
while (booleanExpression)
(block | statement)This closely resembles:if (booleanExpression)
(block | statement)int threeSeries = 1;
while ((threeSeries *=3 ) < 100);
IO.println(threeSeries);Exercise:
-
Find the block or statement mentioned in the previous syntax diagram.
-
What output do you expect?
No. 70
Generating square numbers
|
Q: |
Write an application printing the first ten square numbers. The output should look like: The square of 1 is 1 The square of 2 is 4 The square of 3 is 9 The square of 4 is 16 The square of 5 is 25 The square of 6 is 36 The square of 7 is 49 The square of 8 is 64 The square of 9 is 81 The square of 10 is 100 |
|
A: |
|
No. 71
Calculating factorial
|
Q: |
The factorial of a given integer n is being defined as the following product: In addition the factorial of zero is being defined as: Implement a
|
||||
|
A: |
|
No. 72
Mitigating factorial overflow problems
|
Q: |
The solution of Calculating factorial yields:
Actually is the last correct value here. equals 6227020800 but we see just 1932053504. Whats wrong here? Provide an enhanced solution. |
||||
|
A: |
This is (again) an overflow problem. The largest possible
int value is
or 2,147,483,647. Thus 479,001,600 or
still fits in nicely but 6,227,020,800
corresponding to
does not. We may use long factorial (int value) { long product = 1; while (1 < value) { product *= value; value--; } return product; } This works well until including
and fails again for 18: 6402373705728000
19: 121645100408832000
20: 2432902008176640000
21: -4249290049419214848 |
