Demo entry 6641734

c

   

Submitted by anonymous on Sep 20, 2017 at 21:39
Language: C. Code size: 754 Bytes.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	FILE*fp;
	if((fp=fopen("sum.txt","w"))==NULL)
	{
		printf("cannot open this file\n");
		exit(0);
	}                                               //以w模式打开文件 sum.txt 
	while(1)                                        //第一层while循环:控制x的输入和停止 
	{
		int k=1;
		double x,y=0;
		printf("Please input x:\n");
		scanf("%lf",&x);
		if(getchar()=='E')break;                   //设置循环结束条件:输入字母E(表示end) 
		while(k<=1000000)                          //第二层while循环:控制k,对前1000000项求和 
		{
		    y+=1/(k*(k+x));
		    k++;
		}
		fprintf(fp,"x=%.15lf,y=%.15lf\n",x,y);     //在txt文件中打印x,y的数值
    }
	fclose(fp);                                    //关闭文件 
	getchar();
	return 0; 
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).