直到给定奇数的奇数的平均值?

直到给定奇数为止的奇数平均值是一个简单的概念。您只需要找到奇数,直到该数字,然后求和并除以该数字即可。

如果要找到n之前的奇数平均值。然后我们将发现从1到n的奇数相加,然后将其除以奇数个数。

示例

直到9的奇数平均值为5,即

1 + 3 + 5 + 7 + 9 = 25 => 25/5 = 5

有两种计算奇数的平均值,直到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