- 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?

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

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.

Average of square of first 2 natural numbers is 2.5 ,

1^{2} + 2^{2} = 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 −

#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; }

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 −

#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; }

The average of the square of 2 natural numbers is 2.500000

Advertisements