- Related Questions & Answers
- Average numbers in array in C Programming
- Add minimum number to an array so that the sum becomes even in C programming
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C Program to Check if all digits of a number divide it
- Concatenate a string given number of times in C++ programming
- Superperfect Number in C programming
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- Sum of first n natural numbers in C Program
- HTML DOM Input Number value Property
- HTML DOM Input Number type Property
- How to find the number of days in a month of a particular year in Java?
- Number of elements a particular tag contains in JavaScript?
- Print multiples of Unit Digit of Given Number in C Program
- C/C++ Programming to Count trailing zeroes in factorial of a number?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

To find the average of even numbers till a given even number, we will add all the even number till the given number and t count the number of even numbers. Then divide the sum by the number of even numbers.

Average of even numbers till 10 is 6 i.e.

2 + 4 + 6 + 8 + 10 = 30 => 30/ 5 = 6

There are two methods for calculating the average of even number till n which is an even number.

- Using Loops
- Using Formula

To calculate the average of even numbers till n, we will add all even numbers till n and then divide in by the number of even number till than.

Program to calculate the average of even natural numbers till n −

#include <stdio.h> int main() { int n = 14,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 even numbers till %d is %f",n, average); return 0; }

The average of even numbers till 14 is 8.000000

To calculate the average of even numbers till n we can use a mathematical formula (n+2)/2 where n is an even number which is the given condition in our problem.

Program to calculate the average of n even natural numbers −

#include <stdio.h> int main() { int n = 15; float average = (n+2)/2; printf("The average of even numbers till %d is %f",n, average); return 0; }

The average of even numbers till 14 is 8.000000

Advertisements