current position:Home>C language problem solving best detailed process

C language problem solving best detailed process

2022-01-26 23:53:04 CSDN Q & A

Write a function , Input n For even when , Call function to find 1/2+1/4+...+1/n, When the input n In an odd number of , Call function 1/1+1/3+...+1/n.




Refer to the answer 1:
 #include<stdio.h>int main(){    int n;    double sum = 0,i;    scanf("%d",&n);    if(n % 2 == 1){        i = 1;        while(i <= n){            sum += 1.0/i;            i += 2;        }    }else{        i = 2;        while(i <= n){            sum += 1.0/i;            i += 2;        }    }    printf("%lf",sum);    return 0;}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262353033153.html

Random recommended