Average of squares of natural numbers

Here given code implementation process.

/*
    C program for
    Average of squares of natural numbers
*/
#include <stdio.h>

// Average of series of squares of natural numbers
void averageOfNaturalNo(int n)
{
	long double sum = 0.0;
	double result = 0.0;
	if (n > 0)
	{
		// Calculate sum of squares of n natural numbers
		sum = ((n *(n + 1) *(2 *n + 1)) / 6);
		// Calculate average
		result = sum / n;
	}
	// Display calculated result
	printf(" Average of squares of %d natural number is : %lf \n", 
           n, result);
}
int main(int argc, char
	const *argv[])
{
	/*
	   Test A
	   n = 5
	   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	    
	    55
	    -- = 11
	    5
	*/
	averageOfNaturalNo(5);
	/*
	   Test B
	   n = 8
	   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	   (7)² + (8)²  = 204
	   204
	   ---- = 25.5
	    8
	*/
	averageOfNaturalNo(8);
	return 0;
}

Output

 Average of squares of 5 natural number is : 11.000000
 Average of squares of 8 natural number is : 25.500000
/*
    Java program for
    Average of squares of natural numbers
*/
public class NaturalNumbers
{
	// Average of series of squares of natural numbers
	public void averageOfNaturalNo(int n)
	{
		double sum = 0.0;
		double result = 0.0;
		if (n > 0)
		{
			// Calculate sum of squares of n natural numbers
			sum = ((n * (n + 1) * (2 * n + 1)) / 6);
			// Calculate average
			result = sum / n;
		}
		// Display calculated result
		System.out.print(" Average of squares of " + n + " natural number is : " + result + " \n");
	}
	public static void main(String[] args)
	{
		NaturalNumbers task = new NaturalNumbers();
		/*
		   Test A
		   n = 5
		   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
		    
		    55
		    -- = 11
		    5
		*/
		task.averageOfNaturalNo(5);
		/*
		   Test B
		   n = 8
		   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
		   (7)² + (8)²  = 204
		   204
		   ---- = 25.5
		    8
		*/
		task.averageOfNaturalNo(8);
	}
}

Output

 Average of squares of 5 natural number is : 11.0
 Average of squares of 8 natural number is : 25.5
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program for
    Average of squares of natural numbers
*/
class NaturalNumbers
{
	public:
		// Average of series of squares of natural numbers
		void averageOfNaturalNo(int n)
		{
			double sum = 0.0;
			double result = 0.0;
			if (n > 0)
			{
				// Calculate sum of squares of n natural numbers
				sum = ((n *(n + 1) *(2 *n + 1)) / 6);
				// Calculate average
				result = sum / n;
			}
			// Display calculated result
			cout << " Average of squares of " 
          		<< n << " natural number is : " 
          		<< result << " \n";
		}
};
int main()
{
	NaturalNumbers *task = new NaturalNumbers();
	/*
	   Test A
	   n = 5
	   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	    
	    55
	    -- = 11
	    5
	*/
	task->averageOfNaturalNo(5);
	/*
	   Test B
	   n = 8
	   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	   (7)² + (8)²  = 204
	   204
	   ---- = 25.5
	    8
	*/
	task->averageOfNaturalNo(8);
	return 0;
}

Output

 Average of squares of 5 natural number is : 11
 Average of squares of 8 natural number is : 25.5
// Include namespace system
using System;
/*
    Csharp program for
    Average of squares of natural numbers
*/
public class NaturalNumbers
{
	// Average of series of squares of natural numbers
	public void averageOfNaturalNo(int n)
	{
		double sum = 0.0;
		double result = 0.0;
		if (n > 0)
		{
			// Calculate sum of squares of n natural numbers
			sum = ((n * (n + 1) * (2 * n + 1)) / 6);
			// Calculate average
			result = sum / n;
		}
		// Display calculated result
		Console.Write(" Average of squares of " + 
                      n + " natural number is : " + 
                      result + " \n");
	}
	public static void Main(String[] args)
	{
		NaturalNumbers task = new NaturalNumbers();
		/*
		   Test A
		   n = 5
		   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
		    
		    55
		    -- = 11
		    5
		*/
		task.averageOfNaturalNo(5);
		/*
		   Test B
		   n = 8
		   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
		   (7)² + (8)²  = 204
		   204
		   ---- = 25.5
		    8
		*/
		task.averageOfNaturalNo(8);
	}
}

Output

 Average of squares of 5 natural number is : 11
 Average of squares of 8 natural number is : 25.5
package main
import "fmt"
/*
    Go program for
    Average of squares of natural numbers
*/

// Average of series of squares of natural numbers
func averageOfNaturalNo(n int) {
	var sum float64 = 0.0
	var result float64 = 0.0
	if n > 0 {
		// Calculate sum of squares of n natural numbers
		sum = float64((n * (n + 1) * (2 * n + 1)) / 6.0)
		// Calculate average
		result = sum / float64(n)
	}
	// Display calculated result
	fmt.Print(" Average of squares of ", 
		n, " natural number is : ", result, " \n")
}
func main() {
	
	/*
	   Test A
	   n = 5
	   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	    
	    55
	    -- = 11
	    5
	*/
	averageOfNaturalNo(5)
	/*
	   Test B
	   n = 8
	   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	   (7)² + (8)²  = 204
	   204
	   ---- = 25.5
	    8
	*/
	averageOfNaturalNo(8)
}

Output

 Average of squares of 5 natural number is : 11.0
 Average of squares of 8 natural number is : 25.5
<?php
/*
    Php program for
    Average of squares of natural numbers
*/
class NaturalNumbers
{
	// Average of series of squares of natural numbers
	public	function averageOfNaturalNo($n)
	{
		$sum = 0.0;
		$result = 0.0;
		if ($n > 0)
		{
			// Calculate sum of squares of n natural numbers
			$sum = ((($n * ($n + 1) * (2 * $n + 1)) / 6));
			// Calculate average
			$result = $sum / $n;
		}
		// Display calculated result
		echo(" Average of squares of ".$n.
			" natural number is : ".$result.
			" \n");
	}
}

function main()
{
	$task = new NaturalNumbers();
	/*
	   Test A
	   n = 5
	   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	    
	    55
	    -- = 11
	    5
	*/
	$task->averageOfNaturalNo(5);
	/*
	   Test B
	   n = 8
	   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	   (7)² + (8)²  = 204
	   204
	   ---- = 25.5
	    8
	*/
	$task->averageOfNaturalNo(8);
}
main();

Output

 Average of squares of 5 natural number is : 11
 Average of squares of 8 natural number is : 25.5
/*
    Node JS program for
    Average of squares of natural numbers
*/
class NaturalNumbers
{
	// Average of series of squares of natural numbers
	averageOfNaturalNo(n)
	{
		var sum = 0.0;
		var result = 0.0;
		if (n > 0)
		{
			// Calculate sum of squares of n natural numbers
			sum = (((n * (n + 1) * (2 * n + 1)) / 6));
			// Calculate average
			result = sum / n;
		}
		// Display calculated result
		process.stdout.write(" Average of squares of " + 
                             n + " natural number is : " + 
                             result + " \n");
	}
}

function main()
{
	var task = new NaturalNumbers();
	/*
	   Test A
	   n = 5
	   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	    
	    55
	    -- = 11
	    5
	*/
	task.averageOfNaturalNo(5);
	/*
	   Test B
	   n = 8
	   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	   (7)² + (8)²  = 204
	   204
	   ---- = 25.5
	    8
	*/
	task.averageOfNaturalNo(8);
}
main();

Output

 Average of squares of 5 natural number is : 11
 Average of squares of 8 natural number is : 25.5
#    Python 3 program for
#    Average of squares of natural numbers
class NaturalNumbers :
	#  Average of series of squares of natural numbers
	def averageOfNaturalNo(self, n) :
		sum = 0.0
		result = 0.0
		if (n > 0) :
			#  Calculate sum of squares of n natural numbers
			sum = (((n * (n + 1) * (2 * n + 1)) / 6))
			#  Calculate average
			result = sum / n
		
		#  Display calculated result
		print(" Average of squares of ", 
              n ," natural number is : ", 
              result ," ")
	

def main() :
	task = NaturalNumbers()
	#   Test A
	#   n = 5
	#   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	#    55
	#    -- = 11
	#    5
	task.averageOfNaturalNo(5)
	#   Test B
	#   n = 8
	#   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	#   (7)² + (8)²  = 204
	#   204
	#   ---- = 25.5
	#    8
	task.averageOfNaturalNo(8)

if __name__ == "__main__": main()

Output

 Average of squares of  5  natural number is :  11.0
 Average of squares of  8  natural number is :  25.5
#    Ruby program for
#    Average of squares of natural numbers
class NaturalNumbers 
	#  Average of series of squares of natural numbers
	def averageOfNaturalNo(n) 
		sum = 0.0
		result = 0.0
		if (n > 0) 
			#  Calculate sum of squares of n natural numbers
			sum = ((n * (n + 1) * (2 * n + 1)) / 6.0)
			#  Calculate average
			result = sum / n
		end

		#  Display calculated result
		print(" Average of squares of ", 
              n ," natural number is : ", result ," \n")
	end

end

def main() 
	task = NaturalNumbers.new()
	#   Test A
	#   n = 5
	#   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	#    55
	#    -- = 11
	#    5
	task.averageOfNaturalNo(5)
	#   Test B
	#   n = 8
	#   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	#   (7)² + (8)²  = 204
	#   204
	#   ---- = 25.5
	#    8
	task.averageOfNaturalNo(8)
end

main()

Output

 Average of squares of 5 natural number is : 11.0 
 Average of squares of 8 natural number is : 25.5 
/*
    Scala program for
    Average of squares of natural numbers
*/
class NaturalNumbers()
{
	// Average of series of squares of natural numbers
	def averageOfNaturalNo(n: Int): Unit = {
		var sum: Double = 0.0;
		var result: Double = 0.0;
		if (n > 0)
		{
			// Calculate sum of squares of n natural numbers
			sum = ((n * (n + 1) * (2 * n + 1)) / 6);
			// Calculate average
			result = sum / n;
		}
		// Display calculated result
		print(" Average of squares of " + 
              n + " natural number is : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: NaturalNumbers = new NaturalNumbers();
		/*
		   Test A
		   n = 5
		   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
		    
		    55
		    -- = 11
		    5
		*/
		task.averageOfNaturalNo(5);
		/*
		   Test B
		   n = 8
		   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
		   (7)² + (8)²  = 204
		   204
		   ---- = 25.5
		    8
		*/
		task.averageOfNaturalNo(8);
	}
}

Output

 Average of squares of 5 natural number is : 11.0
 Average of squares of 8 natural number is : 25.5
/*
    Swift 4 program for
    Average of squares of natural numbers
*/
class NaturalNumbers
{
	// Average of series of squares of natural numbers
	func averageOfNaturalNo(_ n: Int)
	{
		var sum: Double = 0.0;
		var result: Double = 0.0;
		if (n > 0)
		{
			// Calculate sum of squares of n natural numbers
			sum = Double((n * (n + 1) * (2 * n + 1)) / 6);
			// Calculate average
			result = sum / Double(n);
		}
		// Display calculated result
		print(" Average of squares of", 
              n ,"natural number is : ", result ," ");
	}
}
func main()
{
	let task: NaturalNumbers = NaturalNumbers();
	/*
	   Test A
	   n = 5
	   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	    
	    55
	    -- = 11
	    5
	*/
	task.averageOfNaturalNo(5);
	/*
	   Test B
	   n = 8
	   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	   (7)² + (8)²  = 204
	   204
	   ---- = 25.5
	    8
	*/
	task.averageOfNaturalNo(8);
}
main();

Output

 Average of squares of 5 natural number is :  11.0
 Average of squares of 8 natural number is :  25.5
/*
    Kotlin program for
    Average of squares of natural numbers
*/
class NaturalNumbers
{
	// Average of series of squares of natural numbers
	fun averageOfNaturalNo(n: Int): Unit
	{
		var sum: Double ;
		var result: Double = 0.0;
		if (n > 0)
		{
			// Calculate sum of squares of n natural numbers
			sum = ((n * (n + 1) * (2 * n + 1)) / 6.0);
			// Calculate average
			result = sum / n;
		}
		// Display calculated result
		print(" Average of squares of " + 
              n + " natural number is : " + 
              result + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: NaturalNumbers = NaturalNumbers();
	/*
	   Test A
	   n = 5
	   (1)² + (2)² + (3)² + (4)² + (5)²  = 55
	    
	    55
	    -- = 11
	    5
	*/
	task.averageOfNaturalNo(5);
	/*
	   Test B
	   n = 8
	   (1)² + (2)² + (3)² + (4)² + (5)² + (6)² + 
	   (7)² + (8)²  = 204
	   204
	   ---- = 25.5
	    8
	*/
	task.averageOfNaturalNo(8);
}

Output

 Average of squares of 5 natural number is : 11.0
 Average of squares of 8 natural number is : 25.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