/***************************
时间:20200306
地点:国防大学
作者:袁欢
程序目的:函数定义及调用
编写函数,求1+1/2+1/3+1/4+...+1/n的值
****************************/
#include<stdio.h>
#include<stdlib.h>
float count(int n)
{
int i;
float sum;
if (n <= 0)
{
printf("The %d is invalid", n);
return - 1;
}
else
{
sum = 0;
for (i = 1; i <= n; i++)
{
sum = sum + 1.0 / i;
}
return sum;
}
}
void main()
{
int n;
float sum;
printf("Please input n:\n");
scanf_s("%d", &n);
sum = count(n);
printf("n=%d s=%f\n", n, sum);
system("pause");
}
思考问题:确定函数有无参数
确定参数个数及类型
确定函数有无返回值
C语言编写函数,求1+1/2+1/3+1/4+...+1/n的值
猜你喜欢
转载自blog.csdn.net/weixin_45713352/article/details/104696317
今日推荐
周排行