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

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>
#include <stdlib.h>


/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int x=1;
double s=0,y;
scanf("%d",&x);

for(y=1;y<=2*x;y+=2){
    s+=1.0/y;
}
printf("sum = %.6f\n",s);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xxl-h/p/11111055.html