How to calculate the sum of a series with the nth term being user input in Java -
i trying compute summation , pi. have gotten pi calculation work, having difficulty on summation. output of summation supposed calculate 1/3 + 3/5 + 5/7 .... number/n nth term user input, uncertain calculation. if input 5 output should calculate 1/3 + 3/5, code add 5 terms 1/3 + 3/5 + 5/7 + 7/9 + 9/11, doing wrong? here code:
import java.util.scanner; public class n01092281 { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("enter nth term series."); double userinput = input.nextint(); double sum = 0.0; for(int = 2; <= userinput*2; i+=2) { sum += ((double)(i-1)/(i+1)); } system.out.printf("the sum of series %12.12f" , sum); double pi = 0; (int counter = 1; counter < userinput; counter++){ pi += 4 * (math.pow(-1,counter + 1)/((2*counter) - 1)); } system.out.printf(" ,the computation of pi %1.12f", pi); } }
i guess, doing right calculation. changed way using i. need update count how many times go.
this how changed it
double userinput = 11; int count =0; if(userinput>=3){ count =(int)( userinput-1)/2; } double sum = 0.0; for(int = 1; <= count; i++) { sum += ((double)(2*i-1)/(2*i+1)); system.out.print((2*i-1)+"/"+(2*i+1)+' '); } system.out.println(); system.out.printf("the sum of series %12.12f" , sum); output:-
1/3 3/5 5/7 7/9 9/11 sum of series 3.243578643579 here printing series, make clear you.
Comments
Post a Comment