Given an integer N, find the absolute difference between sum of the cubes of first N natural numbers and the sum of first N natural numbers.

Input:N = 3Output:30 Sum of first three numbers is 3 + 2 + 1 = 6 Sum of Cube of first three numbers is = 1 + 8 + 27 = 36 Absolute difference = 36 - 6 = 30Input:N = 5Output:210

**Approach:**

- The sum of the cube of first N natural numbers, using the formula:

- The sum of first N numbers, using the formula:

- The absolute difference between both the sums is

where

Below is the implementation of the above approach:

## C++

`// C++ program to find the difference ` `// between the sum of the cubes of the ` `// first N natural numbers and ` `// the sum of the first N natural number ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `difference(` `int` `n) ` `{ ` ` ` ` ` `int` `S, res; ` ` ` ` ` `// Sum of first n natural numbers ` ` ` `S = (n * (n + 1)) / 2; ` ` ` ` ` `// Find the required difference ` ` ` `res = S * (S - 1); ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << difference(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the difference ` `// between the sum of the cubes of the ` `// first N natural numbers and ` `// the sum of the first N natural number ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `difference(` `int` `n) ` `{ ` ` ` ` ` `int` `S, res; ` ` ` ` ` `// Sum of first n natural numbers ` ` ` `S = (n * (n + ` `1` `)) / ` `2` `; ` ` ` ` ` `// Find the required difference ` ` ` `res = S * (S - ` `1` `); ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.print(difference(n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the difference ` `# between the sum of the cubes of the ` `# first N natural numbers and ` `# the sum of the first N natural number ` `def` `difference(n) : ` ` ` ` ` `# Sum of first n natural numbers ` ` ` `S ` `=` `(n ` `*` `(n ` `+` `1` `)) ` `/` `/` `2` `; ` ` ` ` ` `# Find the required difference ` ` ` `res ` `=` `S ` `*` `(S ` `-` `1` `); ` ` ` ` ` `return` `res; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` `; ` ` ` `print` `(difference(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the difference ` `// between the sum of the cubes of the ` `// first N natural numbers and ` `// the sum of the first N natural number ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `difference(` `int` `n) ` `{ ` ` ` `int` `S, res; ` ` ` ` ` `// Sum of first n natural numbers ` ` ` `S = (n * (n + 1)) / 2; ` ` ` ` ` `// Find the required difference ` ` ` `res = S * (S - 1); ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.Write(difference(n)); ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

**Output:**

210

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of cubes of even and odd natural numbers
- Sum of cubes of first n odd natural numbers
- Average of Cubes of first N natural numbers
- Sum of alternating sign cubes of first N Natural numbers
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Possible two sets from first N natural numbers difference of sums as D
- Sum of series formed by difference between product and sum of N natural numbers
- Sum of cubes of first n even numbers
- Number of perfect cubes between two given numbers
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Check whether a number can be represented as difference of two consecutive cubes
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- LCM of First n Natural Numbers
- Natural Numbers
- Sum of first n natural numbers
- Sum of all natural numbers in range L to R
- Sum of first N natural numbers which are divisible by 2 and 7
- Repeated sum of first N natural numbers
- Sum of sum-series of first N Natural numbers
- Sum of fifth powers of the first n natural numbers

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.