Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Average of first n even natural numbers?
The average of first n even natural numbers is the sum of the numbers divided by the count of numbers. The first n even natural numbers are 2, 4, 6, 8, ..., 2n.
Syntax
Average = Sum of first n even numbers / n
You can calculate this using two methods −
Find the sum of n even natural numbers using a loop and divide by n
Use the mathematical formula for direct calculation
Method 1: Using Loop
This method iterates through the first n even numbers, calculates their sum, and then finds the average −
#include <stdio.h>
int main() {
int n = 5;
int sum = 0;
float average = 0;
printf("First %d even natural numbers: ", n);
for (int i = 1; i <= n; i++) {
int evenNum = i * 2;
printf("%d ", evenNum);
sum += evenNum;
}
average = (float)sum / n;
printf("\nSum = %d<br>", sum);
printf("Average = %.2f<br>", average);
return 0;
}
First 5 even natural numbers: 2 4 6 8 10 Sum = 30 Average = 6.00
Method 2: Using Formula
The mathematical formula for the average of first n even natural numbers is (n + 1). This is derived from the sum formula: Sum = n(n+1) and Average = Sum/n = n+1 −
#include <stdio.h>
int main() {
int n = 5;
int average = n + 1;
printf("Using formula: Average of first %d even natural numbers<br>", n);
printf("Formula: Average = n + 1 = %d + 1 = %d<br>", n, average);
printf("Average = %d<br>", average);
return 0;
}
Using formula: Average of first 5 even natural numbers Formula: Average = n + 1 = 5 + 1 = 6 Average = 6
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Loop Method | O(n) | O(1) | Understanding the concept |
| Formula Method | O(1) | O(1) | Large values of n |
Conclusion
The formula method (n+1) is more efficient for calculating the average of first n even natural numbers, especially for larger values of n. The loop method helps understand the underlying concept but becomes slower as n increases.
