C语言平方根

功能是:求 n 以内(不包括 n)同时能被 3 与 7 整除的所有自然数之和的平方根 s,并作为函数值返回,后结果 s 输出到文件 out.dat 中。
例如若 n 为 1000 时,函数值应为:s=153.909064。
#include <conio.h>

#include<math.h>

#include <stdio.h>

double countValue(int n)

{ int i;

double s=0.0;

for(i=1;i<n;i++)

if(i%21==0) s+=i;

return sqrt(s);

}

main()

{

clrscr();

printf(“自然数之和的平方根=%f\n”,countValue(1000));

progReadWrite();

}

progReadWrite()

{

FILE *fp,*wf;

int i,n;

float s;

fp=fopen(“in.dat”,“r”);

if(fp==NULL){

printf(“数据文件 in.dat 不存在!”);

return;

}

wf=fopen(“out.dat”,“w”);

for(i=0;i<10;i++){

fscanf(fp,"%d\n",&n);

s=countValue(n);

fprintf(wf,"%f\n",s);

}

fclose(fp);

fclose(wf);

}

发布了239 篇原创文章 · 获赞 3 · 访问量 3157

猜你喜欢

转载自blog.csdn.net/it_xiangqiang/article/details/105176402