It would be nice if the user could say how many terms to sum up.
The user might want to see the sum of the first 10 terms, for example:
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10
Here is the program again, with new blanks for the improvements.
Now the number of terms to sum up is entered by the user.
import java.util.Scanner ; public class HarmonicTester { public static void main ( String[] args ) { Scanner scan = new Scanner(System.in); System.out.print("How many Terms? "); int limit = scan.nextInt(); int term=1 ; double sum = 0.0; while ( term <= ) { sum += 1.0/term; // add the next term to sum term++ ; // increment term } System.out.println("Sum of " + limit + " terms:" + sum ); } }
Fill in the blanks to complete the program.