用C语言求三个整型数的和、积和平均值
时间: 2024-09-29 17:07:11 浏览: 107
在C语言中,你可以使用标准库函数`scanf`读取用户输入的三个整数,并通过基本的算术运算来计算它们的和、积和平均值。以下是一个简单的示例程序:
```c
#include <stdio.h>
int main() {
int a, b, c;
float sum, product, average;
// 读取用户输入
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
// 计算和、积和平均值
sum = a + b + c; // 和
product = a * b * c; // 积
average = (float)(sum) / 3; // 平均值(注意类型转换)
// 输出结果
printf("三个数的和是:%d\n", sum);
printf("三个数的积是:%d\n", product);
printf("三个数的平均值是:%f\n", average);
return 0;
}
```
当你运行这个程序时,它会提示你依次输入三个整数,然后计算并显示它们的和、积和平均值。
相关问题
C语言求三个整形变量abc的平均值,abc由键盘输入
在C语言中,你可以通过以下几个步骤来计算三个整数变量a、b和c的平均值,并允许用户从键盘输入这三个数字:
1. 首先,需要包含头文件`stdio.h`以便使用标准输入输出函数。
2. 定义一个函数,例如`calculate_average()`,用于接收三个整数作为参数。
```c
#include <stdio.h>
double calculate_average(int a, int b, int c) {
double average = (double)(a + b + c) / 3; // 将整数相加后再除以3得到平均值,使用双精度浮点数防止整数溢出
return average;
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c); // 使用scanf从键盘读取用户输入
double result = calculate_average(a, b, c);
printf("三个数的平均值是: %.2f\n", result); // 输出结果保留两位小数
return 0;
}
```
c语言三个数求平均值
在C语言中,可以通过定义一个函数来求三个整数的平均值。函数的参数为三个整型数,返回值为平均值。在主函数中,读取三个整数,调用函数求平均值并输出结果。以下是一个示例代码:
```
#include <stdio.h>
double avg(int a, int b, int c) {
double sum = a + b + c;
double avg = sum / 3;
return avg;
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
double result = avg(a, b, c);
printf("这三个数的平均数是:%.2f", result);
return 0;
}
```
该代码中,avg函数用于求三个整数的平均值,main函数中读取三个整数,调用avg函数求平均值并输出结果。其中,%.2f表示输出结果保留两位小数。
阅读全文
相关推荐
















