Average of Squares of n Natural Numbers?

Given a number n, we need to find the average of the squares of the first n natural numbers. We calculate the square of each number from 1 to n, sum all these squares, and divide by n to get the average.

Syntax

average = (1² + 2² + 3² + ... + n²) / n

Example

Let's calculate the average of squares for the first 3 natural numbers −

#include <stdio.h>

int main() {
    int n = 3;
    int sum = 0;
    
    /* Calculate sum of squares */
    for(int i = 1; i <= n; i++) {
        sum += i * i;
    }
    
    /* Calculate and display average */
    double average = (double)sum / n;
    printf("Sum of squares: %d<br>", sum);
    printf("Average of squares: %.6f<br>", average);
    
    return 0;
}
Sum of squares: 14
Average of squares: 4.666667

Explanation

For n = 3:

  • 1² = 1
  • 2² = 4
  • 3² = 9
  • Sum = 1 + 4 + 9 = 14
  • Average = 14/3 = 4.666667

Using Formula Approach

We can also use the mathematical formula for sum of squares: n(n+1)(2n+1)/6 −

#include <stdio.h>

int main() {
    int n = 5;
    
    /* Using formula: n(n+1)(2n+1)/6 */
    int sum = n * (n + 1) * (2 * n + 1) / 6;
    double average = (double)sum / n;
    
    printf("n = %d<br>", n);
    printf("Sum of squares using formula: %d<br>", sum);
    printf("Average of squares: %.6f<br>", average);
    
    return 0;
}
n = 5
Sum of squares using formula: 55
Average of squares: 11.000000

Conclusion

The average of squares of n natural numbers can be calculated by either iterating through all numbers or using the mathematical formula. The formula approach is more efficient for larger values of n.

Updated on: 2026-03-15T11:35:01+05:30

399 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements