Exercise 2-14 seeking odd-number sequence and the first N (15 minutes)

This problem requires programming, calculates the sequence 1 + 1/3 + 1/5 + ... and the first N.

Input formats:

In a given input row positive integer N.

Output formats:

And output portion of the value S in accordance with the "sum = S" format in a row, six decimal place. Title ensure that the calculation result does not exceed the double precision.

Sample input:

23
 

Sample output:

sum = 2.549541

#include<stdio.h>
int main(void){
    int N,i,k;
    double Sum;
    
    scanf("%d",&N);
    i=0,Sum=0,k=1; 
    while(i<N){
        Sum + = 1.0 / k;
        i++;
        k+=2;
    } 
    printf("sum = %.6f",Sum);
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Kimsohyun/p/12578863.html