Have to write a program in Java that sums the following summation: from 1 / (1+S
ID: 3550443 • Letter: H
Question
Have to write a program in Java that sums the following summation: from 1 / (1+Sqrt(2))
+1 / (Sqrt(2)+Sqrt(3)) + 1 / (Sqrt(3)+Sqrt(4)).......1 / (Sqrt(624)+Sqrt(625)).
Here is what I have so far. Am I on the right track?
import java.util.Scanner;
public class GetCalcSum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double sum = 1;
double calculation = 0;
for (int i = 0; i <= 624; i++) {
calculation = (1 / (Math.pow(i, (1 / 2)) + Math.pow(i + 1, (1 / 2))));
sum += i;
}
System.out.printf("sum is: %0.8d", calculation);
}
}
Out put that it returns is 0.5. And when I do the same summation on a calculator as Sigma(1 / (Sqrt(x)+Sqrt(x+1)) ) from 1-624 the result is 24.
Explanation / Answer
Your Errors:
1.You are storing terms in calculation variable and final value in sum, so why are you outputting calculation, display sum instead.
2. Be careful with JAVA type casting. You are using int as i while each terms are in double
JAVA Code:
import java.util.Scanner;
public class GetCalcSum {
public static void main(String[] args) {
Scanner br=new Scanner(System.in);
double sum = 0.0;
double calculation;
for (int i = 1; i <= 624; i++) {
calculation = (1.0 / ((Math.pow((double)(i), (1.0 / 2.0)) + Math.pow((double)(i + 1), (1.0 / 2.0)))));
sum += calculation;
}
System.out.println("sum is: "+sum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.