do ... while
Enter value, 0 to terminate: 3 Enter value, 0 to terminate: 1 Enter value, 0 to terminate: 0 Sum: 4 |
|
whileloop:-
Termination condition being checked before possible first loop execution.
Remark: Depending on its termination condition a
whileloop may not be entered at all. do ... whileloop:-
Termination condition being checked after first loop execution.
Remark: A
do ... whileloop is being entered at least once.
No. 73
Even or odd?
|
Q: |
Write an application which asks for integer values telling its user whether a given value is even or odd. Providing the special value 0 shall terminate your application: Enter an integer (0 to terminate): 77
77 is an odd number
Enter an integer (0 to terminate): -3
-3 is an odd number
Enter an integer (0 to terminate): 26
26 is an even number
Enter an integer (0 to terminate): 0
0 is an even number
Goodbye!Tip
|
|
A: |
We obviously need a loop to ask for further input unless the last entered value was 0. In any case the loop's statement will be executed at least once: Enter an integer (0 to terminate): 0
0 is an even number
Goodbye!A |
No. 74
Square root approximation
|
Q: |
Derived from the Newton–Raphson method we can approximate a given value 's square root by the following recursively defined series:
Implement the following method sqrt(...) for calculating a given value's square root: TipDue to the limited precision of machine arithmetics you may continue until your approximation value no longer changes. Then test your implementation by e.g.: Enter a non-negative value: 2.0 The square root of 2.0 is close to 1.414213562373095 It's square is 1.9999999999999996 The difference appearing here is inevitable due to
limited precision when dealing with |
|
A: |
We introduce two variables |
