/* * Calculate the sum of the series 1/1+1/2+1/3+...+1/n * Suggested exercises: * - Change the while-loop to do-loop or for-loop. * - Use an outer loop (nested loops) for n=1,2,4,8,...,2^m to print a table of n and sum * - Use a conditional print (i % 100 == 0) instead of nested loops to print the table (compare efficiency). * - Use powers of 2 in the conditional print (Math.log(i)/Math.log(2) % 2 == 0). * - Find the first n for which sum >= 10. * - Modify the series to 1/(i*i) and try the previous item (convergence experiments). */ import java.util.Scanner; class Series { public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.print ("Type the number of terms in the series: "); int n = scan.nextInt(); int i; double sum; i = 1; sum = 0; while (i<=n) { sum = sum + 1/(double)i; i++; } System.out.println(n+"\t"+sum); } }