Zzuli OJ 1041: Sequence sum 2

Source: Internet
Author: User
Description

Enter an integer n, output sequence 1-1/3+1/5-...... The and of the first n items. Input

Enter only one integer n.
Output

The result retains 2 as a decimal and occupies a single row. Sample Input 3 sample Output 0.87 HINT

Source


#include <stdio.h>

int main ()
{
    int i, n;
    Double sum, Deno, flag;

    scanf ("%d", &n);
    sum = 0.0;
    Deno = 1.0; The initial value is the denominator of the first item
    flag = 1.0;  The initial value is the symbol for the first item for

    (i = 1; I <= n; i++)//Loop n times
    {
        sum + = Flag/deno; calculate and add the current item
        Deno + = 2;  Computes the next denominator
        flag =-flag//calculates the next symbol
    }

    printf ("%.2f\n", sum);
    return 0;
}


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.