Difference between Sum of Cubes and Sum of First N Natural Numbers
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 = 3 Output: 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 = 30 Input: N = 5 Output: 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 Codeint main(){ int n = 5; cout << difference(n); return 0;} |
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 numberclass 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 Codepublic static void main(String[] args){ int n = 5; System.out.print(difference(n));}}// This code is contributed by 29AjayKumar |
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 numberdef difference(n) : # Sum of first n natural numbers S = (n * (n + 1)) // 2; # Find the required difference res = S * (S - 1); return res;# Driver Codeif __name__ == "__main__" : n = 5; print(difference(n)); # This code is contributed by AnkitRai01 |
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 numberusing 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 Codestatic public void Main (){ int n = 5; Console.Write(difference(n));}}// This code is contributed by ajit |
Javascript
<script>// JavaScript 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 function difference(n) { let S, res; // Sum of first n natural numbers S = Math.floor((n * (n + 1)) / 2); // Find the required difference res = S * (S - 1); return res; } // Driver Code let n = 5; document.write(difference(n)); //This code is contributed by Surbhi Tyagi</script> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


