Posted on by Kalkicode
Code Mathematics

Average of first n natural numbers

Calculating the average of the first n natural numbers is a basic mathematical problem that involves finding the sum of the first n integers and then dividing that sum by n. This problem showcases how a simple mathematical formula can be used to find meaningful results.

Problem Statement

Given a positive integer n, the problem is to find the average of the first n natural numbers.

Idea to Solve the Problem

The sum of the first n natural numbers can be expressed using the well-known formula: 1 + 2 + 3 + ... + n. The formula for the sum of the first n natural numbers is given by:

Sum = 1 + 2 + 3 + ... + n = (n * (n + 1)) / 2

The average of these numbers can be calculated by dividing the sum by n.

Pseudocode

function findAverage(n)
    if n <= 0
        return
    
    sum = (n * (n + 1)) / 2
    average = sum / n
    
    print average

Algorithm Explanation

  1. The findAverage function takes a positive integer n as input.

  2. If n is less than or equal to 0, the function returns immediately to handle invalid inputs.

  3. Calculate the sum of the first n natural numbers using the formula (n * (n + 1)) / 2.

  4. Calculate the average by dividing the sum by n.

  5. Print the calculated average.

Program Solution

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

void findAverage(int n)
{
	if (n <= 0)
	{
		return;
	}
	// Calculate sum of n natural number
  	// Formula = (n × (n + 1)) / 2.0
	double sum = (n * (n + 1)) / 2.0;
	// Calculate Average
	double average = sum / n;
	// Display calculated result
	printf("\n %lf", average);
}
int main()
{
	int n = 6;
	/*
	    n = 6
	    ---------------------------
	    1 + 2 + 3 + 4 + 5 + 6 = (21)
	    ----------------------------
	    21
	    -- = 3.5
	     6 

	*/
	findAverage(n);
	return 0;
}

Output

 3.500000
// Java program for
// Average of first n natural numbers
public class Average
{
	public void findAverage(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate sum of n natural number
		// Formula = (n × (n + 1)) / 2.0
		double sum = (n * (n + 1)) / 2.0;
		// Calculate Average
		double average = sum / n;
		// 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 = (21)
		    ----------------------------
		    21
		    -- = 3.5
		     6 

		*/
		task.findAverage(n);
	}
}

Output

 3.5
// Include header file
#include <iostream>

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

Output

 3.5
// Include namespace system
using System;
// Csharp program for
// Average of first n natural numbers
public class Average
{
	public void findAverage(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate sum of n natural number
		// Formula = (n × (n + 1)) / 2.0
		double sum = (n * (n + 1)) / 2.0;
		// Calculate Average
		double average = sum / n;
		// 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 = (21)
		    ----------------------------
		    21
		    -- = 3.5
		     6 
		*/
		task.findAverage(n);
	}
}

Output

 3.5
package main
import "fmt"
// Go program for
// Average of first n natural numbers
type Average struct {}
func getAverage() * Average {
	var me *Average = &Average {}
	return me
}
func(this Average) findAverage(n int) {
	if n <= 0 {
		return
	}
	// Calculate sum of n natural number
	// Formula = (n × (n + 1)) / 2.0
	var sum float64 = float64(n * (n + 1)) / 2.0
	// Calculate Average
	var average float64 = sum / float64(n)
	// 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 = (21)
	    ----------------------------
	    21
	    -- = 3.5
	     6 
	*/
	task.findAverage(n)
}

Output

 3.5
<?php
// Php program for
// Average of first n natural numbers
class Average
{
	public	function findAverage($n)
	{
		if ($n <= 0)
		{
			return;
		}
		// Calculate sum of n natural number
		// Formula = (n × (n + 1)) / 2.0
		$sum = ($n * ($n + 1)) / 2.0;
		// Calculate Average
		$average = $sum / $n;
		// Display calculated result
		echo("\n ".$average);
	}
}

function main()
{
	$task = new Average();
	$n = 6;
	/*
	    n = 6
	    ---------------------------
	    1 + 2 + 3 + 4 + 5 + 6 = (21)
	    ----------------------------
	    21
	    -- = 3.5
	     6 
	*/
	$task->findAverage($n);
}
main();

Output

 3.5
// Node JS program for
// Average of first n natural numbers
class Average
{
	findAverage(n)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate sum of n natural number
		// Formula = (n × (n + 1)) / 2.0
		var sum = (n * (n + 1)) / 2.0;
		// Calculate Average
		var average = sum / n;
		// 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 = (21)
	    ----------------------------
	    21
	    -- = 3.5
	     6 
	*/
	task.findAverage(n);
}
main();

Output

 3.5
#  Python 3 program for
#  Average of first n natural numbers
class Average :
	def findAverage(self, n) :
		if (n <= 0) :
			return
		
		#  Calculate sum of n natural number
		#  Formula = (n × (n + 1)) / 2.0
		sum = (n * (n + 1)) / 2.0
		#  Calculate Average
		average = sum / n
		#  Display calculated result
		print("\n ", average, end = "")
	

def main() :
	task = Average()
	n = 6
	#    n = 6
	#    ---------------------------
	#    1 + 2 + 3 + 4 + 5 + 6 = (21)
	#    ----------------------------
	#    21
	#    -- = 3.5
	#     6 
	task.findAverage(n)

if __name__ == "__main__": main()

Output

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

		#  Calculate sum of n natural number
		#  Formula = (n × (n + 1)) / 2.0
		sum = (n * (n + 1)) / 2.0
		#  Calculate Average
		average = sum / n
		#  Display calculated result
		print("\n ", average)
	end

end

def main() 
	task = Average.new()
	n = 6
	#    n = 6
	#    ---------------------------
	#    1 + 2 + 3 + 4 + 5 + 6 = (21)
	#    ----------------------------
	#    21
	#    -- = 3.5
	#     6 
	task.findAverage(n)
end

main()

Output

 3.5
// Scala program for
// Average of first n natural numbers
class Average()
{
	def findAverage(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Calculate sum of n natural number
		// Formula = (n × (n + 1)) / 2.0
		var sum: Double = (n * (n + 1)) / 2.0;
		// Calculate Average
		var average: Double = sum / n;
		// 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 = (21)
		    ----------------------------
		    21
		    -- = 3.5
		     6 
		*/
		task.findAverage(n);
	}
}

Output

 3.5
// Swift 4 program for
// Average of first n natural numbers
class Average
{
	func findAverage(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate sum of n natural number
		// Formula = (n × (n + 1)) / 2.0
		let sum: Double = Double(n * (n + 1)) / 2.0;
		// Calculate Average
		let average: Double = sum / Double(n);
		// 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 = (21)
	    ----------------------------
	    21
	    -- = 3.5
	     6 
	*/
	task.findAverage(n);
}
main();

Output

  3.5
// Kotlin program for
// Average of first n natural numbers
class Average
{
	fun findAverage(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		// Calculate sum of n natural number
		// Formula = (n × (n + 1)) / 2.0
		val sum: Double = (n * (n + 1)) / 2.0;
		// Calculate Average
		val average: Double = sum / n;
		// 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 = (21)
	    ----------------------------
	    21
	    -- = 3.5
	     6 
	*/
	task.findAverage(n);
}

Output

 3.5

Time Complexity

The time complexity of this algorithm is constant (O(1)) because the calculations involve basic arithmetic operations that do not depend on the input size.

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.

New Comment