|
|
solutions chapter 2
The following program calculates eulers number iteratively. Note that we change the output precision to 10 digits. Also we compare the calculated value with a value returned by a call to the exponential function of the math library. For the use of the double-type see chapter 3. What happens if we change the "e+=1.0/nf" in "e+=1/nf"? #include <iostream>
One possible solution is given by the following program. The outer for-loop loops over the numbers from 3 to 100. The inner while-loop increases the variable j, until either i is dividable by j without rest, or j reaches i-1. If after this loop j has the value of i-1 it means, that we did not found a number by which i may be divided without rest and so for i is a prime number. #include <iostream> Of course one can construct different loops with different control structures. Like in this example there is often more than one way of realizing simple algorithms. |
email me: Daniel Schürmann |