# Average of Squares of Natural Numbers

Given a number n, find the average of square of natural Numbers till n

Examples :

```Input : n = 2
Output :2.5
12 + 22 = 2.5

Input  : n = 3
Output : 4.666667
12 + 22 + 32 = 4.666667
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach :
A naive approach will be to run a loop from 1 to n and and find the average of sum up all the squares.

## C

 `// C program to calculate 1^2+2^2+3^2+... average ` `// of square number ` `#include ` ` `  `// Function to calculate  average of square number ` `float` `AvgofSquareN(``int` `n) ` `{ ` `    ``float` `sum = 0; ` `    ``for` `(``int` `i = 1; i <= n; i++) ` `        ``sum += (i * i); ` `    ``return` `sum/n; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 2; ` `    ``printf``(``"%f"``, AvgofSquareN(n)); ` `    ``return` `0; ` `} `

## Java

 `// Java program to calculate 1^2+2^2+3^2+  ` `// ... average of square number ` `import` `java.io.*; ` ` `  `public` `class` `GFG{ ` `     `  `// Function to calculate average  ` `// of square number ` `static` `float` `AvgofSquareN(``int` `n) ` `{ ` `    ``float` `sum = ``0``; ` `    ``for` `(``int` `i = ``1``; i <= n; i++) ` `        ``sum += (i * i); ` `    ``return` `sum / n; ` `}  ` ` `  `// Driver code ` `static` `public` `void` `main (String[] args) ` `{ ` `    ``int` `n = ``2``; ` `    ``System.out.println(AvgofSquareN(n)); ` ` `  `} ` `} ` ` `  `// This code is contributed by vt_m.  `

## Python3

 `# Python program to  ` `# calculate 1^2+2^2+3^2+...  ` `# average of square number ` ` `  `# Function to calculate  ` `# average of square number ` `def` `AvgofSquareN(n) : ` ` `  `    ``sum` `=` `0` `    ``for` `i ``in` `range``(``1``, n ``+` `1``) : ` `        ``sum` `+``=` `(i ``*` `i) ` `    ``return` `sum``/``n ` ` `  `# Driver code ` `n ``=` `2` `print` `(AvgofSquareN(n)) ` `     `  `# This code is contributed by  ` `# Manish Shaw(manishshaw1) `

## C#

 `// C# program to calculate 1^2+2^2+3^2+ ` `// ... average of square number ` `using` `System; ` ` `  `public` `class` `GFG{ ` `     `  ` `  `// Function to calculate average ` `// of square number ` `static` `float` `AvgofSquareN(``int` `n) ` `{ ` `    ``float` `sum = 0; ` `    ``for` `(``int` `i = 1; i <= n; i++) ` `        ``sum += (i * i); ` `    ``return` `sum / n; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main (String []args) ` `{ ` `    ``int` `n = 2; ` `    ``Console.WriteLine(AvgofSquareN(n)); ` `     `  `} ` `} ` ` `  `// This code is contributed by vt_m.  `

## PHP

 ` `

Output :

```2.500000
```

Efficient Approach : Sum of squares of natural numbers (n + 1)(2n + 1) / 6. Therefore average is n(n + 1)(2n + 1) / 6 * n = (n + 1)(2n + 1) / 6.

## C

 `// C program to get the  Average of Square ` `// of first n natural numbers ` `#include ` ` `  `float` `AvgofSquareN(``int` `n) ` `{ ` `    ``return` `(``float``)((n + 1) * (2 * n + 1)) / 6; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` `    ``printf``(``"%f"``, AvgofSquareN(n)); ` `    ``return` `0; ` `} `

## Java

 `// Java program to get the Average of  ` `// square of first n natural numbers ` `import` `java.io.*; ` ` `  `public` `class` `GFG{ ` ` `  `// Function to get the average ` `static` `float` `AvgofSquareN(``int` `n) ` `{ ` `    ``return` `(``float``)((n + ``1``) * (``2` `*  ` `                    ``n + ``1``)) / ``6``; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `main (String[] args) ` `{ ` `    ``int` `n = ``2``; ` `    ``System.out.println(AvgofSquareN(n)); ` ` `  `} ` `} ` ` `  `// This code is contributed by vt_m.   `

## Python3

 `# PHP program to get the Average of  ` `# Square of first n natural numbers ` `  `  `def` `AvgofSquareN(n) : ` ` `  `    ``return` `((n ``+` `1``) ``*` `(``2` `*` `n ``+` `1``)) ``/` `6``; ` ` `  `# Driver Code ` `n ``=` `2``; ` `print` `(AvgofSquareN(n)); ` `  `  `# This code is contributed by Manish Shaw ` `# (manishshaw1) `

## C#

 `// C# program to get the Average of  ` `// squareof first n natural numbers ` `using` `System; ` ` `  `public` `class` `GFG{ ` ` `  `// Function to calculate average ` `static` `float` `AvgofSquareN(``int` `n) ` `{ ` `    ``return` `(``float``)((n + 1) * (2 *  ` `                    ``n + 1)) / 6; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main (String []args) ` `{ ` `    ``int` `n = 2; ` `    ``Console.WriteLine(AvgofSquareN(n)); ` `} ` `} ` ` `  `// This code is contributed by vt_m.  `

## PHP

 ` `

Output :

```2.500000
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, manishshaw1

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.