编写一个函数求1+1/2+1/3+...+1/n的值

/*革启博客,革启网,袁欢,袁欢的博客,袁欢博客
版本:vs2019社区版
功能;编写一个函数求1+1/2+1/3+...+1/n的值
*/
#include<stdio.h>
float count(int n)
{
    int i;
    float sum;
    if (n <= 0)
    {
        printf("非法数据!\n");
        return -1;
    }
    else
    {
        sum = 0;
        for(i = 1; i <= n; i++)
        {
            sum = sum + 1 / i;
        }
        return sum;
    }
}
void main()
{
    int n;
    float sum;
    printf("请输入n的值:\n");
    scanf_s("%d", &n);
    sum = count(n);
    printf("n=%d    sum=%f\n", n, sum);
}

猜你喜欢

转载自www.cnblogs.com/qq1480040000/p/13376575.html