Cycle-26. The first n and four of the given sequence (15)

Source: Internet
Author: User

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.819
Import 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.