do ... while
Enter value, 0 to terminate: 3 Enter value, 0 to terminate: 1 Enter value, 0 to terminate: 0 Sum: 4 |
No. 64
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. 65
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:
Create a program which interactively asks a user for a positive value and calculate it's square root like 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 TipDue to the limited precision of machine arithmetics you may continue until your approximation value no longer changes. |
A: |
We introduce two variables
|