Average of cubes of first n natural numbers

Here given code implementation process.

// C Program
// Average of cubes of first n natural numbers
#include <stdio.h>

void findAverageOfCube(int n)
{
	if (n <= 0)
	{
		return;
	}
	// Calculate cube of n natural number
	// Formula = (n×(n+1)²) / 4
	double average = (n * (n + 1) * (n + 1)) / 4.0;
	// Display calculated result
	printf("\n %lf", average);
}
int main()
{
	int n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 

	*/
	findAverageOfCube(n);
	return 0;
}

Output

 73.500000
// Java program for
// Average of cubes of first n natural numbers
public class Average
{
	public void findAverageOfCube(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		double average = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		System.out.print("\n " + average);
	}
	public static void main(String[] args)
	{
		Average task = new Average();
		int n = 6;
		/*
		    n = 6
		    ---------------------------
		    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
		    ----------------------------
		    440
		    -- = 73.5
		     6 

		*/
		task.findAverageOfCube(n);
	}
}

Output

 73.5
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Average of cubes of first n natural numbers
class Average
{
	public: void findAverageOfCube(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		double average = (n *(n + 1) *(n + 1)) / 4.0;
		// Display calculated result
		cout << "\n " << average;
	}
};
int main()
{
	Average *task = new Average();
	int n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task->findAverageOfCube(n);
	return 0;
}

Output

 73.5
// Include namespace system
using System;
// Csharp program for
// Average of cubes of first n natural numbers
public class Average
{
	public void findAverageOfCube(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		double average = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		Console.Write("\n " + average);
	}
	public static void Main(String[] args)
	{
		Average task = new Average();
		int n = 6;
		/*
		    n = 6
		    ---------------------------
		    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
		    ----------------------------
		    440
		    -- = 73.5
		     6 
		*/
		task.findAverageOfCube(n);
	}
}

Output

 73.5
package main
import "fmt"
// Go program for
// Average of cubes of first n natural numbers
type Average struct {}
func getAverage() * Average {
	var me *Average = &Average {}
	return me
}
func(this Average) findAverageOfCube(n int) {
	if n <= 0 {
		return
	}
	// Calculate cube of n natural number
	// Formula = (n×(n+1)²) / 4
	var average float64 = float64(n * (n + 1) * (n + 1)) / 4.0
	// Display calculated result
	fmt.Print("\n ", average)
}
func main() {
	var task * Average = getAverage()
	var n int = 6
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n)
}

Output

 73.5
<?php
// Php program for
// Average of cubes of first n natural numbers
class Average
{
	public	function findAverageOfCube($n)
	{
		if ($n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		$average = ($n * ($n + 1) * ($n + 1)) / 4.0;
		// Display calculated result
		echo("\n ".$average);
	}
}

function main()
{
	$task = new Average();
	$n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	$task->findAverageOfCube($n);
}
main();

Output

 73.5
// Node JS program for
// Average of cubes of first n natural numbers
class Average
{
	findAverageOfCube(n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		var average = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		process.stdout.write("\n " + average);
	}
}

function main()
{
	var task = new Average();
	var n = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n);
}
main();

Output

 73.5
#  Python 3 program for
#  Average of cubes of first n natural numbers
class Average :
	def findAverageOfCube(self, n) :
		if (n <= 0) :
			return
		
		#  Calculate cube of n natural number
		#  Formula = (n×(n+1)²) / 4
		average = (n * (n + 1) * (n + 1)) / 4.0
		#  Display calculated result
		print("\n ", average, end = "")
	

def main() :
	task = Average()
	n = 6
	#    n = 6
	#    ---------------------------
	#    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	#    ----------------------------
	#    440
	#    -- = 73.5
	#     6 
	task.findAverageOfCube(n)

if __name__ == "__main__": main()

Output

  73.5
#  Ruby program for
#  Average of cubes of first n natural numbers
class Average 
	def findAverageOfCube(n) 
		if (n <= 0) 
			return
		end

		#  Calculate cube of n natural number
		#  Formula = (n×(n+1)²) / 4
		average = (n * (n + 1) * (n + 1)) / 4.0
		#  Display calculated result
		print("\n ", average)
	end

end

def main() 
	task = Average.new()
	n = 6
	#    n = 6
	#    ---------------------------
	#    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	#    ----------------------------
	#    440
	#    -- = 73.5
	#     6 
	task.findAverageOfCube(n)
end

main()

Output

 73.5
// Scala program for
// Average of cubes of first n natural numbers
class Average()
{
	def findAverageOfCube(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		var average: Double = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		print("\n " + average);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Average = new Average();
		var n: Int = 6;
		/*
		    n = 6
		    ---------------------------
		    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
		    ----------------------------
		    440
		    -- = 73.5
		     6 
		*/
		task.findAverageOfCube(n);
	}
}

Output

 73.5
// Swift 4 program for
// Average of cubes of first n natural numbers
class Average
{
	func findAverageOfCube(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		let average: Double = Double(n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		print("\n ", average, terminator: "");
	}
}
func main()
{
	let task: Average = Average();
	let n: Int = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n);
}
main();

Output

  73.5
// Kotlin program for
// Average of cubes of first n natural numbers
class Average
{
	fun findAverageOfCube(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate cube of n natural number
		// Formula = (n×(n+1)²) / 4
		val average: Double = (n * (n + 1) * (n + 1)) / 4.0;
		// Display calculated result
		print("\n " + average);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Average = Average();
	val n: Int = 6;
	/*
	    n = 6
	    ---------------------------
	    1³ + 2³ + 3³ + 4³ + 5³ + 6³ = (440)
	    ----------------------------
	    440
	    -- = 73.5
	     6 
	*/
	task.findAverageOfCube(n);
}

Output

 73.5


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.

New Comment







© 2021, kalkicode.com, All rights reserved