here is the problem:
here is what I have:Write a program that computes the following sum:
sum = 1.0/1 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5 + .... + 1.0/N
N is an integer limit that the user enters.
Enter N
4
Sum is: 2.08333333333
import java.util.Scanner; class nSum { public static void main ( String[] args ) { Scanner scan = new Scanner ( System.in ); System.out.println("Enter an integer: "); int N = scan.nextInt(); double sum = 1.0; double count = 0.0; while( N > 0 ) { count = count + 1.0; sum = sum / count; N = (N - 1); } System.out.println("The sum is: " + sum); System.out.println(""); } }
what am I doing wrong here? as far as I can tell the problem is within the while loop but the math looks right. when it's run, it seems to be doing integer math instead of floating point math and I have no idea why. any help would be greatly appreciated! :/