Posted on by Kalkicode
Code Mathematics

# Sum of cubes of n even natural numbers

The problem involves calculating the sum of the cubes of the first 'n' even natural numbers. An even natural number is a positive integer that is divisible by 2. The task is to find the sum of the cubes of the first 'n' even natural numbers using a specific formula.

## Problem Statement

Given a positive integer 'n', the goal is to calculate and display the sum of the cubes of the first 'n' even natural numbers using a predetermined formula.

## Example

Let's take an example to illustrate the problem:

## Example

• Given 'n' = 3 (the first 3 even natural numbers are 2, 4, and 6)
• We need to find the sum of the cubes of these even numbers.

## Idea to Solve

The formula for calculating the sum of the cubes of the first 'n' even natural numbers is: 2 × (n × (n + 1))^2.

## Pseudocode

``````function findSumOfCubes(n):
if n <= 0:
return
sum = 2 * ((n * (n + 1)) * (n * (n + 1)))
print(sum)

# Example usage
n = 3
findSumOfCubes(n)``````

## Algorithm Explanation

1. The `findSumOfCubes` function takes 'n' as input.
2. It first checks if 'n' is less than or equal to 0. If it is, the function returns.
3. It then calculates the sum of cubes using the formula provided.
4. The formula involves squaring the product of 'n' and 'n + 1', then multiplying by 2.
5. The calculated sum is printed as output.

## Code Solution

``````// C Program
// Sum of cubes of n even natural numbers
#include <stdio.h>

void findSunOfCube(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
double sum = 2 *((n *(n + 1)) *(n *(n + 1)));
// Display calculated result
printf("\n %lf", sum);
}
int main()
{
int n = 3;
/*
n = 3
*/
findSunOfCube(n);
return 0;
}``````

#### Output

`` 288.000000``
``````// Java program for
// Sum of cubes of n even natural numbers
public class Sum
{
public void findSunOfCube(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
double sum = 2 * ((n * (n + 1)) * (n * (n + 1)));
// Display calculated result
System.out.print("\n " + sum );
}
public static void main(String[] args)
{

int n = 3;
/*
n = 3
*/
}
}``````

#### Output

`` 288.0``
``````// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Sum of cubes of n even natural numbers
class Sum
{
public: void findSunOfCube(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
double sum = 2 *((n *(n + 1)) *(n *(n + 1)));
// Display calculated result
cout << "\n " << sum;
}
};
int main()
{
int n = 3;
/*
n = 3
*/
return 0;
}``````

#### Output

`` 288``
``````// Include namespace system
using System;
// Csharp program for
// Sum of cubes of n even natural numbers
public class Sum
{
public void findSunOfCube(int n)
{
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
double sum = 2 * ((n * (n + 1)) * (n * (n + 1)));
// Display calculated result
Console.Write("\n " + sum);
}
public static void Main(String[] args)
{
int n = 3;
/*
n = 3
*/
}
}``````

#### Output

`` 288``
``````package main
import "fmt"
// Go program for
// Sum of cubes of n even natural numbers
type Sum struct {}
func getSum() * Sum {
var me *Sum = &Sum {}
return me
}
func(this Sum) findSunOfCube(n int) {
if n <= 0 {
return
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
sum := 2 * ((n * (n + 1)) * (n * (n + 1)))
// Display calculated result
fmt.Print("\n ", sum)
}
func main() {
var task * Sum = getSum()
var n int = 3
/*
n = 3
*/
}``````

#### Output

`` 288``
``````<?php
// Php program for
// Sum of cubes of n even natural numbers
class Sum
{
public	function findSunOfCube(\$n)
{
if (\$n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
\$sum = 2 * ((\$n * (\$n + 1)) * (\$n * (\$n + 1)));
// Display calculated result
echo("\n ".\$sum);
}
}

function main()
{
\$n = 3;
/*
n = 3
*/
}
main();``````

#### Output

`` 288``
``````// Node JS program for
// Sum of cubes of n even natural numbers
class Sum
{
findSunOfCube(n)
{
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
var sum = 2 * ((n * (n + 1)) * (n * (n + 1)));
// Display calculated result
process.stdout.write("\n " + sum);
}
}

function main()
{
var n = 3;
/*
n = 3
*/
}
main();``````

#### Output

`` 288``
``````#  Python 3 program for
#  Sum of cubes of n even natural numbers
class Sum :
def findSunOfCube(self, n) :
if (n <= 0) :
return

#  Calculate sum of cube of n natural number
#  Formula : 2×(n×(n+1))²
sum = 2 * ((n * (n + 1)) * (n * (n + 1)))
#  Display calculated result
print("\n ", sum, end = "")

def main() :
n = 3
#    n = 3

if __name__ == "__main__": main()``````

#### Output

``  288``
``````#  Ruby program for
#  Sum of cubes of n even natural numbers
class Sum
def findSunOfCube(n)
if (n <= 0)
return
end

#  Calculate sum of cube of n natural number
#  Formula : 2×(n×(n+1))²
sum = 2 * ((n * (n + 1)) * (n * (n + 1)))
#  Display calculated result
print("\n ", sum)
end

end

def main()
n = 3
#    n = 3
end

main()``````

#### Output

`` 288``
``````// Scala program for
// Sum of cubes of n even natural numbers
class Sum()
{
def findSunOfCube(n: Int): Unit = {
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
var sum: Double = 2 * ((n * (n + 1)) * (n * (n + 1)));
// Display calculated result
print("\n " + sum);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Sum = new Sum();
var n: Int = 3;
/*
n = 3
*/
}
}``````

#### Output

`` 288.0``
``````// Swift 4 program for
// Sum of cubes of n even natural numbers
class Sum
{
func findSunOfCube(_ n: Int)
{
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
let sum: Int = 2 * ((n * (n + 1)) * (n * (n + 1)));
// Display calculated result
print("\n ", sum, terminator: "");
}
}
func main()
{
let n: Int = 3;
/*
n = 3
*/
}
main();``````

#### Output

``  288``
``````// Kotlin program for
// Sum of cubes of n even natural numbers
class Sum
{
fun findSunOfCube(n: Int): Unit
{
if (n <= 0)
{
return;
}
// Calculate sum of cube of n natural number
// Formula : 2×(n×(n+1))²
val sum = 2 * ((n * (n + 1)) * (n * (n + 1)));
// Display calculated result
print("\n " + sum);
}
}
fun main(args: Array < String > ): Unit
{
val n: Int = 3;
/*
n = 3
*/
}``````

#### Output

`` 288``

## Time Complexity

The time complexity of the code is constant, O(1), since the calculations involved in the formula do not depend on the input size 'n'. Regardless of the value of 'n', the number of operations remains the same.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post