The subject asks to write the program, computes the sequence 1-1/4 + 1/7-1/10 + ... The sum of the first n items.
Input format:
The input gives a positive integer n in a row.
Output format:
The output part and the value s in the format of "sum = S" in a row are accurate to 3 digits after the decimal point. The title guarantees that the calculation result does not exceed the double precision range.
Input Sample:
10
Sample output:
sum = 0.819Import Java.math.bigdecimal;import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner c in = new Scanner (system.in);d ouble n = cin.nextint ();d ouble sum = 0;for (Double i = 1.0, j = 1; I <= N; i++, J + = 3) {i F (i% 2 = 0) {sum + = 1/j;} else {sum-= 1/j;}} BigDecimal bd = new BigDecimal (sum); bd = Bd.setscale (3, bigdecimal.round_half_up); System.out.println ("sum =" +bd);}}
Cycle-26. The first n and four of the given sequence (15)