MODE ——计算了 任意多个数字的平均值(知识点:for的循环)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42167759/article/details/83652425

问题描述:

输入浮点数值 判断是否继续输入 输出N时候 退出for循环 计算出来 输入所有数字的平均值

运行结果:

[root@J01051386 C]# ./a.out 

This program calculates the average of any number of values.
Enter a value:1
Do you want to enter another value?(Y or N):y

Enter a value:2
Do you want to enter another value?(Y or N):y

Enter a value:3
Do you want to enter another value?(Y or N):n

The average is 2.00

代码部分:

#include <stdio.h>
#include <ctype.h>
int main(void)
{
	char answer = 'N';
	double total = 0.0;
	double value = 0.0;
	unsigned int count = 0;
	
	printf("\nThis program calculates the average of any number of values.");
//for 循环中的break 的使用	
	for (;;)
	{	
		printf("\nEnter a value:");
		scanf(" %lf",&value);
		total += value;
		++count;
		
		printf("Do you want to enter another value?(Y or N):");
		scanf(" %c",&answer);
		if(tolower(answer) == 'n')
			break;
	}	
	printf("\nThe average is %.2lf\n",total/count);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42167759/article/details/83652425