JAVA Sum 1/i^2 from 1 to n, where n=1000. Write code to sum the formula (1/i2) o
ID: 3757074 • Letter: J
Question
JAVA
Sum 1/i^2 from 1 to n, where n=1000.
Write code to sum the formula (1/i2) over all values of i in the range [1...n]. We'll test it for n=1000 and other values.
You should not read anything from the keyboard.
In class SumInverseOfISquared you must make a method sum() that computes the sum of this formula
- sum() returns a double
- sum() takes one input parameter, an integer n that is the upper limit of i.
public class SumInverseOfISquared {
// TODO: Write your sum() method here.
// Main method to invoke the sum() and print the result.
public static void main(String[] args) {
SumInverseOfISquared obj = new SumInverseOfISquared();
double value = obj.sum(1000);
System.out.println("Sum of 1/i^2 for i in [1...1000] is: " + value);
}
}
Explanation / Answer
public class SumInverseOfISquared { // TODO: Write your sum() method here. public static double sum(int n) { double total = 0; for(int i = 1; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.