Sum of Cubes of First N Natural Numbers in C#



Problem Description

We are given a number N, and we need to calculate the sum of the cubes of the first N natural numbers. In this article, we are going to learn how we can find the sum of cubes of the first N natural numbers C#.

Example 1

  • Input: N = 3
  • Output: 36

Explanation:

The cubes of the first 3 natural numbers are:
13 = 1, 238, 33 = 27
The sum of these cubes is:1 + 8 + 27 = 36

Example 2

  • Input: N = 5
  • Output: 225

Explanation:

The cubes of the first 3 natural numbers are:
13 = 1, 23 = 8, 33 = 27 , 43 = 64 , 53 = 125
The sum of these cubes is:
1 + 8 + 27 + 64 + 125 = 225

Below are different approaches for finding the sum of cubes of the first N natural numbers

Using Iterative Approach

This is a simple and direct approach to find the sum of cubes. We use a loop to calculate the cube of each number from 1 to N and add it to a cumulative sum variable.

Steps for Implementation

  1. Initialize a variable sum to 0.
  2. Loop through each number from 1 to N.
  3. For each number, calculate its cube and add it to the sum.
  4. Return the final value of the sum.

Implementation Code

using System;

class Program {
  static int SumOfCubesIterative(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      sum += i * i * i;
    }
    return sum;
  }

  static void Main() {
    // input
    int N = 3;
    int result = SumOfCubesIterative(N);
    Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
  }
}

Output:

The sum of cubes of the first 3 natural numbers is: 36

Time Complexity: O(N)
Space Complexity: O(1)

Using Formula-Based Approach

In this method, we use a mathematical formula to find the sum of cubes without iterating through numbers. The formula for the sum of cubes of the first N natural numbers is:

Sum = ( N×(N+1) / 2)2

Steps for Implementation 

  1. Create a function.
  2. Inside the function use the formula to calculate the sum of the first N natural numbers.
  3. Square the result to find the sum of cubes and return it.

?Implementation Code

using System;

class Program {
  static int SumOfCubesFormula(int n) {
    // Calculate sum using the formula
    int sum = (n * (n + 1) / 2);
    return sum * sum;
  }

  static void Main() {
    int N = 5; // Example input
    int result = SumOfCubesFormula(N);
    Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
  }
}

Output:

The sum of cubes of the first 5 natural numbers is: 225

Time Complexity: O(1)
Space Complexity: O(1)

Using Recursive Approach

In this approach, we use recursive algorithm to calculate the sum of cubes. For each recursive call, we add the cube of the current number to the result of the remaining numbers.

Steps for Implementation 

  1. Define a recursive function SumofCubesRecursive.
  2. Define a base case: If n = 0, return 0.
  3. For recursive base case return cube of n + recursive function( n- 1) , i.e. n3 + SumofCubesRecursive(n - 1).

Implementation Code

using System;

class Program {
  static int SumOfCubesRecursive(int n) {
    // Base case
    if (n == 0) return 0;

    // Recursive case
    return n * n * n + SumOfCubesRecursive(n - 1);
  }

  static void Main() {
    int N = 4; // Example input
    int result = SumOfCubesRecursive(N);
    Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
  }
}

Output:

The sum of cubes of the first 4 natural numbers is: 100

Time Complexity: O(N)
Space Complexity: O(N)

Real-Life Applications

  • Mathematical Operations:
    It is common to use calculation of powers and sum in mathematical computations.
  • Scientific Analysis:
    It is used in physics and engineering calculations that involve cubic relationships.
  • Data Analysis:
    It is used to find aggregate values and is used in statistical models involving cubes or polynomial terms.
Updated on: 2025-01-24T16:21:48+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements