Summing a Series: Is this correct??
Write a program to sum the following series:
(1/3) + (3/5) + (5/7) + (7/9) + (9/11) + (11/13) + ... + (95/97) + (97/99)
This is my attempt.
Code:
public class SummingSeries {
public static void main(String[] args) {
double sum = 0.0;
for(double i = 1; i <= 99; i++) {
sum += ((i+2)/(i+4));
}
System.out.println("The sum is " + sum);
}
}
This is the output I get.
Quote:
The sum is 92.73308433232398
Is my output correct? I have no idea what the desired output is supposed to be so I need to know if 92.73308433232398 is actually the sum of the series mentioned above. Is there anything wrong with my code? I think my code is correct but I'm not sure.
Re: Summing a Series: Is this correct??
Quote:
Originally Posted by
son012189
Write a program to sum the following series:
(1/3) + (3/5) + (5/7) + (7/9) + (9/11) + (11/13) + ... + (95/97) + (97/99)
This is my attempt.
Code:
public class SummingSeries {
public static void main(String[] args) {
double sum = 0.0;
for(double i = 1; i <= 99; i++) {
sum += ((i+2)/(i+4));
}
System.out.println("The sum is " + sum);
}
}
This is the output I get.
Is my output correct? I have no idea what the desired output is supposed to be so I need to know if 92.73308433232398 is actually the sum of the series mentioned above. Is there anything wrong with my code? I think my code is correct but I'm not sure.
Take a few examples for 'i', say 1, 2 and 3 and see if you get the correct terms: 3/5, 4/6, 5/7; what is that 4/6 doing there?
kind regards,
Jos
Re: Summing a Series: Is this correct??
I see my mistake now thanks. I fixed my code.
Code:
public class SummingSeries {
public static void main(String[] args) {
double sum = 0.0;
for(double i = 1; i <= 49; i++) {
sum += ((2*i-1)/(2*i+1));
}
System.out.println("The sum is " + sum);
}
}
Is my code correct now? or is there something else I need to fix?
Re: Summing a Series: Is this correct??
Quote:
Originally Posted by
son012189
I see my mistake now thanks. I fixed my code.
Code:
public class SummingSeries {
public static void main(String[] args) {
double sum = 0.0;
for(double i = 1; i <= 49; i++) {
sum += ((2*i-1)/(2*i+1));
}
System.out.println("The sum is " + sum);
}
}
Is my code correct now? or is there something else I need to fix?
It's fine now; you also could've done this:
Code:
for (double i= 1; i <= 97; i+= 2)
sum+= i/(i+2);
kind regards,
Jos
Re: Summing a Series: Is this correct??