奇数的平均数直到给定的奇数是一个简单的概念。您只需要找到直到该数字的奇数,然后将它们相加并除以该数字。
如果要找到直到n的奇数的平均数。然后我们将从1到n找到奇数,然后相加,再除以奇数的数量。
例子
奇数的平均数直到9是5,即
1 + 3 + 5 + 7 + 9 = 25 => 25/5 = 5
计算奇数的平均数直到n有两种方法,其中n是一个奇数
- 使用循环
- 使用公式
程序查找到n为止的奇数的平均值,使用循环
为了计算直到n的奇数的平均值,我们将把直到n的所有数字相加,然后除以直到n的奇数的个数。
计算平均值的程序奇自然数直到 n -
示例代码
实时演示
'#include <stdio.h>
int main() {
int n = 15,count = 0;
float sum = 0;
for (int i = 1; i <= n; i++) {
if(i%2 != 0) {
sum = sum + i;
count++;
}
}
float average = sum/count;
printf("The average of odd numbers till %d is %f",n, average);
return 0;
}
输出
'The average of odd numbers till 15 is 8.000000
使用公式计算直到 n 的奇数的平均值
要计算直到 n 的奇数的平均值,我们可以使用数学公式 (n+1)/2,其中 n 是奇数,这是我们问题中的给定条件。
计算奇数自然数直到 n 的平均值的程序 -
示例代码
现场演示
'#include <stdio.h>
int main() {
int n = 15;
float average = (n+1)/2;
printf("The average of odd numbers till %d is %f",n, average);
return 0;
}
输出
'The average of odd numbers till 15 is 8.000000