输入n个正数,计算这些数的算术平均值和几何平均值。c语言
时间: 2023-05-27 14:04:24 浏览: 96
#include <stdio.h>
#include <math.h>
int main() {
int n;
float sum = 0, product = 1;
printf("输入正数个数:");
scanf("%d", &n);
float nums[n];
for (int i = 0; i < n; i++) {
printf("输入第%d个数:", i + 1);
scanf("%f", &nums[i]);
sum += nums[i];
product *= nums[i];
}
float arithmetic_mean = sum / n;
float geometric_mean = pow(product, 1.0/n);
printf("算术平均值为:%.2f\n", arithmetic_mean);
printf("几何平均值为:%.2f", geometric_mean);
return 0;
}
相关问题
输入n个正数,计算这些数的算术平均值和几何平均值 c语言实现
#include <stdio.h>
#include <math.h>
int main() {
int n, i;
float sum = 0, product = 1, num;
printf("请输入n个正数:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("请输入第%d个数:", i);
scanf("%f", &num);
sum += num;
product *= num;
}
printf("算术平均值为:%.2f\n", sum / n);
printf("几何平均值为:%.2f\n", pow(product, 1.0 / n));
return 0;
}
c语言代码输入n个正数,计算这些数的算术平均值和几何平均值
算术平均值是每个数相加后除以总数,而几何平均值是所有数的乘积的n次方根。以下是计算算术平均值和几何平均值的C语言代码:
```
#include <stdio.h>
#include <math.h>
int main()
{
int n, i;
float sum = 0, product = 1, arithmetic_mean, geometric_mean;
printf("Enter the number of values: ");
scanf("%d", &n);
float values[n];
printf("Enter the values: ");
for(i = 0; i < n; i++)
{
scanf("%f", &values[i]);
sum += values[i];
product *= values[i];
}
arithmetic_mean = sum / n;
geometric_mean = pow(product, 1.0/n);
printf("Arithmetic Mean = %f\n", arithmetic_mean);
printf("Geometric Mean = %f\n", geometric_mean);
return 0;
}
```
在此代码中,我们首先输入要计算的数字数量n。然后,我们使用一个for循环来输入每个数字并计算它们的总和和乘积。最后,我们计算算术平均值和几何平均值,并将结果打印到屏幕上。
阅读全文