# Difference between sum of the squares of first n natural numbers and square of sum

Given an integer n, find the absolute difference between sum of the squares of first n natural numbers and square of sum of first n natural numbers.

Examples :

```Input : n = 3
Output : 22.0
Sum of first three numbers is 3 + 2 + 1 = 6
Square of the sum =  36
Sum of squares of first three is 9 + 4 + 1 = 14
Absolute difference = 36 - 14 = 22

Input : n = 10
Output : 2640.0
```

## Recommended: Please solve it on PRACTICE first, before moving on to the solution.

Approach :
1. Find the sum of square of first n natural numbers.
2. Find the sum of first n numbers and square it.
3. Find the absolute difference between both the sums and print it.

Below is the implementation of above approach :

## C++

 `// C++ program to find the difference ` `// between sum of the squares of the ` `// first n natural numbers and square ` `// of sum of first n natural number ` `#include ` `using` `namespace` `std; ` ` `  `int` `Square_Diff(``int` `n){ ` ` `  `int` `l, k, m; ` ` `  `    ``// Sum of the squares of the ` `    ``// first n natural numbers is ` `    ``l = (n * (n + 1) * (2 * n + 1)) / 6; ` `     `  `    ``// Sum of n naturals numbers ` `    ``k = (n * (n + 1)) / 2; ` ` `  `    ``// Square of k ` `    ``k = k * k; ` `     `  `    ``// Differences between l and k ` `    ``m = ``abs``(l - k); ` `     `  `    ``return` `m; ` ` `  `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` `    ``cout << Square_Diff(n); ` `    ``return` `0; ` `     `  `} ` ` `  `// This code is contributed by 'Gitanjali' . `

## Java

 `// Java program to find the difference ` `// between sum of the squares of the ` `// first n natural numbers and square ` `// of sum of first n natural number ` ` `  `public` `class` `GfG{ ` ` `  `static` `int` `Square_Diff(``int` `n){ ` ` `  `int` `l, k, m; ` `    ``// Sum of the squares of the ` `    ``// first n natural numbers is ` `    ``l = (n * (n + ``1``) * (``2` `* n + ``1``)) / ``6``; ` `     `  `    ``// Sum of n naturals numbers ` `    ``k = (n * (n + ``1``)) / ``2``; ` ` `  `    ``// Square of k ` `    ``k = k * k; ` `     `  `    ``// Differences between l and k ` `    ``m = Math.abs(l - k); ` `     `  `    ``return` `m; ` ` `  `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String s[]) ` `{ ` `    ``int` `n = ``10``; ` `    ``System.out.println(Square_Diff(n));      ` `     `  `} ` `}  ` `// This code is contributed by 'Gitanjali'. `

## Python

 `# Python3 program to find the difference ` `# between sum of the squares of the ` `# first n natural numbers and square ` `# of sum of first n natural number ` ` `  `def` `Square_Diff(n): ` ` `  `    ``# sum of the squares of the ` `    ``# first n natural numbers is ` `    ``l ``=` `(n ``*` `(n ``+` `1``) ``*` `(``2` `*` `n ``+` `1``)) ``/` `6` `     `  `    ``# sum of n naturals numbers ` `    ``k ``=` `(n ``*` `(n ``+` `1``)) ``/` `2` ` `  `    ``# square of k ` `    ``k ``=` `k ``*``*` `2` `     `  `    ``# Differences between l and k ` `    ``m ``=` `abs``(l ``-` `k) ` `     `  `    ``return` `m ` ` `  `# Driver code ` `print``(Square_Diff(``10``)) `

## C#

 `// C# program to find the difference ` `// between sum of the squares of the ` `// first n natural numbers and square ` `// of sum of first n natural number ` `using System; ` ` `  `public class GfG{ ` ` `  `static int Square_Diff(int n){ ` ` `  `int l, k, m; ` `    ``// Sum of the squares of the ` `    ``// first n natural numbers is ` `    ``l = (n * (n + 1) * (2 * n + 1)) / 6; ` `     `  `    ``// Sum of n naturals numbers ` `    ``k = (n * (n + 1)) / 2; ` ` `  `    ``// Square of k ` `    ``k = k * k; ` `     `  `    ``// Differences between l and k ` `    ``m = Math.Abs(l - k); ` `     `  `    ``return m; ` ` `  `} ` ` `  `// Driver Code ` `public static void Main() ` `{ ` `    ``int n = 10; ` `       ``Console.Write(Square_Diff(n));      ` `     `  `} ` `}  ` `// This code is contributed by 'vt_m'. `

## PHP

 ` `

Output :

`2640`

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

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.