"Every mathematician knows that the sum of the series
1 + 1/2 + 1/3 + 1/4 + ...
is infinite. But computers are funny things. Write a program which adds up the numbers in this series until the result of adding another one doesn’t change the sum. Use float variables for the arithmetic. Also, find out how many terms in the series are used.
[ The answer is 15.403683 and the number of terms added is 2097152. ] "
I have got this far:
class Terms {
public static void main(String[] args) {
int n=1;
float lastFx = 0;
float fx=1;
while (lastFx != fx) {
n++;
lastFx = 1/n;
fx = 1/(n+1);
}
System.out.println("number of terms used: " + n);
}
}
from this, all the system prints out is: "number of terms used: 2" or "number of terms is 3", or 0.
Why can't i get this program working? :/
Any help is much appreciated


Sign In
Create Account

Back to top









