Open In App

C Program for cube sum of first n natural numbers

Improve
Improve
Like Article
Like
Save
Share
Report

Write a C program to print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till the n-th term. 

Examples:

Input: n = 5
Output: 225
Explanation: 13 + 23 + 33 + 43 + 53 = 225

Input: n = 7
Output: 784
Explanation: 13 + 23 + 33 + 43 + 53 + 63 + 73 = 784

C Program for cube sum of first n natural numbers using Naive Approach:

  • Define the function sumOfCubes() which takes an integer input n.
  • Declare and initialize a variable sum to 0.
  • Start a loop with i ranging from 1 to n.
    • For each iteration of the loop, calculate the cube of i and add it to sum.
  • After the loop ends, return the value of sum.

Below is the implementation of the above approach:

C




// C program for the above approach
#include <stdio.h>
 
// Function to calculate the sum of cubes of numbers from 1
// to n
int sumOfCubes(int n)
{
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        // Cube of the current number is added to the sum
        sum += i * i * i;
    }
    return sum;
}
 
// Driver Code
int main()
{
    // Set the value of n
    int n = 5;
    // Calculate the sum of cubes
    int result = sumOfCubes(n);
    printf("%d\n", result);
    return 0;
}


Output

225

Time complexity: O(n), where n is the input value. 
Auxiliary space: O(1),

C Program for cube sum of first n natural numbers using Mathematical Formula:

An efficient solution is to use direct mathematical formula which is (n ( n + 1 ) / 2) ^ 2.

How does this formula work?

We can prove the formula using mathematical induction. We can easily see that the formula holds true for n = 1 and n = 2. Let this be true for n = k-1.

Let the formula be true for n = k-1.
Sum of first (k-1) natural numbers =
[((k – 1) * k)/2]2

Sum of first k natural numbers =
= Sum of (k-1) numbers + k3
= [((k – 1) * k)/2]2 + k3
= [k2(k2 – 2k + 1) + 4k3]/4
= [k4 + 2k3 + k2]/4
= k2(k2 + 2k + 1)/4
= [k*(k+1)/2]2

Illustration:

For n = 5 sum by formula is
(5*(5 + 1 ) / 2)) ^ 2
= (5*6/2) ^ 2
= (15) ^ 2
= 225

For n = 7, sum by formula is
(7*(7 + 1 ) / 2)) ^ 2
= (7*8/2) ^ 2
= (28) ^ 2
= 784

Below is the implementation of the above approach: 

C




// C program for the above approach
#include <stdio.h>
 
// Function to calculate the sum of the series with cubes of
// the first n natural numbers
int sumOfSeries(int n)
{
    // Calculate the sum of the first n natural numbers
    int x = (n * (n + 1) / 2);
    // Square the result to get the sum of the cubes of the
    // series
    return x * x;
}
 
// Drivers Code
int main()
{
    // Set the value of n
    int n = 5;
    // Calculate the sum of the series
    int result = sumOfSeries(n);
    printf("%d\n", result);
    return 0;
}


Output

225

Time Complexity : O(1) 
Auxiliary Space : O(1) because constant space has been used.

The above program causes overflow, even if result is not beyond integer limit. Like previous post, we can avoid overflow upto some extent by doing division first.

Below is the implementation of the above approach:

C




#include <stdio.h>
 
// Function to calculate the sum of first n natural numbers
int sumOfSeries(int n)
{
    int x;
    if (n % 2 == 0)
        x = (n / 2) * (n + 1);
    else
        x = ((n + 1) / 2) * n;
    return x * x;
}
 
// Drivers Code
int main()
{
    int n = 5;
    printf("%d\n", sumOfSeries(n));
    return 0;
}


Output

225

Time complexity: O(1) since performing constant operations
Auxiliary Space: O(1)
Please refer complete article on Program for cube sum of first n natural numbers for more details!



Last Updated : 24 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads