Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Help on displaying select sums for summation

$
0
0
I finished the code, I'm just confused on how to display certain sums for the summation. I need to only display the "i"th term of S(i) = 2,3,4,5,6,7, and 10. I do not know how to target these numbers because none of the sums equate to exactly 2. The closest one to 2 would be 2.08333.. My code so far is:
public class Lab8s {

	//summation method
	public static double S(int n) {
		double result = 0;
		double sum = 0;
		for (int i = 1; i <= n; i ++) {
			sum += (1.0 / i);
		}

		return sum;
	}

	//main method
	public static void main(String[] args) {
		System.out.println("i\t\tS(i)");
		for (int i = 1; i <= 13000; i++)
			System.out.println(i + "\t\t" + S(i));
	}

}


Here are the first 5 terms that are outputted.
i		S(i)
1		1.0
2		1.5
3		1.8333333333333333
4		2.083333333333333
5		2.283333333333333


Like I said, I don't know how to specifically target the number, 2. The same applies for 3,4,5,6,7, and 10.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>