The average of the square of Natural Number is calculated by adding all the squares up to n natural numbers and then dividing it by the number.
Sample
Average of square of first 2 natural numbers is 2.5 ,
12 + 22 = 5 => 5/2 = 2.5.
There are two methods for calculating this is programming −
- Using Loops
- Using Formula
Calculating average of square of natural numbers using loops
This logic works by finding the squares of all natural numbers. By loop from 1 to n finding square of each and adding to the sum variable. Then dividing this sum by n.
Program to find the sum of squares of natural numbers −
Example Code
#include <stdio.h> int main() { int n = 2; float sum = 0; for (int i = 1; i <= n; i++) { sum = sum + (i * i); } float average = sum/n; printf("The average of the square of %d natural numbers is %f", n,average); return 0; }
Output
The average of the square of 2 natural numbers is 2.500000
Calculating average of square of natural numbers using formula.
There are mathematical formulas to make calculations easy. For calculating the sum of squares of natural numbers the formula is ‘ n*(n+1)*((2*n)+1)/6’ diving this by the number n gives the formula : ‘ (n+1)*((2*n)+1)/6’.
Program to find the sum of squares of natural numbers −
Example Code
#include <stdio.h> int main() { int n = 2; float average = ((n+1)*((2*n)+1)/6); printf("The average of the square of %d natural numbers is %f", n,average); return 0; }
Output
The average of the square of 2 natural numbers is 2.500000