- Related Questions & Answers
- Average numbers in array in C Programming
- C Program to Multiply two Floating Point Numbers?
- 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
- Sum of squares of first n natural numbers in C Program?
- Sum of squares of the first n even numbers in C Program
- Find all the numbers in a string using regular expression in Python
- Lambda expression in Python to rearrange positive and negative numbers
- Array sum after dividing numbers from previous?
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?
- An interesting solution to get all prime numbers smaller than n?
- An Interesting Method to Generate Binary Numbers from 1 to n?
- All possible numbers of N digits and base B without leading zeros?
- All possible binary numbers of length n with equal sum in both halves?
- All palindrome numbers in a list?

Average or mean of n even natural number is the sum of numbers divided by the numbers.

You can calculate this by two methods &minus

Find the sum of n even natural numbers and divide it by number, Using loop.

- Find the sum of n even natural numbers and divide it by number, Using formula.

Find the sum of even natural numbers using a loop that counts up to the number we want the sum. Then we will divide it by n.

#include <stdio.h> int main(void) { int n = 5; int sum = 0; int average = 0; for (int i = 1; i <= n ; i++) { sum += (i*2); } average = sum / n; printf("The average of %d even natural numbers is %d", n,average); return 0; }

The average of 5 even natural numbers is 6

Find the sum of even natural numbers using a mathematical Formula that directly calculates the average.

The formula is (n + 1) = n*(n + 1 )/ n..

#include <stdio.h> int main(void) { int n = 5; int average = n+1 ; printf("The average of %d even natural numbers is %d", n,average); return 0; }

The average of 5 even natural numbers is 6

The second method that uses formula is better because in cases with larger value of n, the loop will run n time will increase the time.

Advertisements