Program for Sum of Cubes of n Natural Numbers

Overview
This exercise will demonstrate how to obtain the sum of cubes of n natural numbers. Here, a single for loop from 1 to n is being used. We compute the term's cube in each step before adding it to the total. This program executes in O(n) time. However, we may use this series formula given below to solve it in O(1) or constant time.

The formula for the sum of cubes of the first n even natural numbers is:
The formula for the sum of cubes of the first n odd natural numbers is:
The Sum of the Cube of First n Natural Numbers
Print the sum of series till n-th term.
Example
Examples of the series are as follows:
Input: n = 2
Output: 9
Input: n = 5
Output: 225
Solution:
A simple solution is to add terms one by one.
Implementation
Adding Terms One by One
Simple C++ program to find the sum of series with cubes of first n natural numbers:
- Initialize a variable to store the sum and iterate from 1 to n, adding the cube of each natural number to the sum in each iteration.
- After the loop finishes, the sum will contain the result of the series sum of the cubes of the first n natural numbers.
Simple Java program to find the sum of series with cubes of first n natural numbers:
Simple Python program to find the sum of series with cubes of first n natural numbers:
Output:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Using Formula
The direct mathematical formula
2 is an effective way to solve the problem.
For n = 5 sum by formula is:
For n = 7, sum by formula is:
A formula-based C++ program to find the sum of a series with cubes of first n natural numbers:
A formula-based Java program to find the sum of series with cubes of first n natural numbers:
A formula-based Python program to find the sum of series with cubes of first n natural numbers:
Output:
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Program to Find Sum of Cube of N Even Natural Numbers
Find the Sum of the First n Even Natural Numbers of Given n.
Examples:
Input: 2
Output: 72
Input: 8
Output: 10368
Finding the sum of the cubes by iterating through n even numbers is easy.
Simple C++ method to find the sum of cubes of first n even numbers:
Java program to perform sum of cubes of first n even natural numbers:
Python3 program to find the sum of cubes of first n even numbers:
Output
- Time Complexity: O(n)
- Auxiliary Space: O(1)
To solve the problem effectively, use the formula below.
We are aware that is the sum of the cubes of the first n natural numbers.
Sum of cubes of first n natural numbers=
Example
Efficient C++ method to find the sum of cubes of first n even numbers:
Java program to perform sum of cubes of first n even natural numbers:
Python3 program to find the sum of cubes of first n even numbers:
Output:
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Program to Find Sum of Cube of N Odd Natural Numbers
Find the Sum of the First n Odd Natural Numbers Given n.
Input: 2
Output: 28
Input: 4
Output: 496
Finding the sum of the cubes by iterating through n odd numbers is an easy solution.
Simple C++ method to find the sum of cubes of first n odd numbers:
Java program to perform sum of cubes of first n odd natural numbers:
Python3 program to find sum of cubes of first n odd numbers:
C# program to perform sum of cubes of first n odd natural numbers:
Output
Complexity Analysis
Time Complexity: O(n), since the cubeSum() function only does one traverse.
Space Complexity: O(1)
Applying the formula below will result in an effective solution.
How Does It Function?
We are aware that
is the sum of the cubes of the first n natural numbers.
Sum of cubes of first n odd natural numbers = Sum of cubes of first 2n natural numbers - Sum of cubes of first n even natural numbers
Efficient C++ method to find sum of cubes of first n odd numbers:
Java program to perform sum of cubes of first n odd natural numbers:
Python3 program to find the sum of cubes of first n odd numbers:
Output:
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
FAQs
Q. What is the sum of the first n natural number cubes?
A. The counting of numbers that begin with 1 and proceed to infinity is known as natural numbers. Finding the sum of the cubes of the first n natural numbers involves combining the cubes of a given number of natural integers beginning with 1. For instance, you can write the sum of the cubes of the first five natural numbers as
, the first ten natural numbers as
, and so on.
Q. What is the formula for the sum of cubes of n natural numbers?
A. The formula for the sum of cubes of n natural integers is as follows: The formula to calculate the sum is,
if we have a series of cubes of n natural numbers.

where n is the total number of natural numbers, counting from 1, that have been used.
Q. Pseudo code or steps calculating the sum of cubes of n natural numbers?
A. We will use a simple iterative method in which any loop, such as a for-loop, while-loop, or do-while loop, may be used:
- From 1 to n times, iterate i.
- For every i find it’s a cube.
- Continue to add each cube to the sum variable.
- Return the sum variable.
- Print the result.
Conclusion
- A program to find the sum of cubes of n natural numbers is a common computational task that involves summing the cubes of integers from 1 to n.
- It can be implemented using a simple loop structure, typically a for or while loop, to iterate through the natural numbers from 1 to n.
- For each natural number in the range, its cube is calculated and added to a running sum variable, which stores the cumulative sum of cubes.
- The loop continues until all natural numbers from 1 to n have been processed, resulting in the sum of the cubes.
- Efficient algorithms can optimize this calculation by using the formula for the sum of cubes, which reduces the time complexity from O(n) to O(1) for finding the sum of cubes of the first n natural numbers.