In this tutorial we will see How to calculate Mean Of First n Natural Numbers . First See sum of n natural numbers formula
Sum = n*(n+1)/2
Now we calculate average of n natural numbers as
average of n natural numbers = sum of n natural number / n
average of n natural numbers=n*(n+1)/2/n
average of n natural numbers = n+1/2
Hence , mean of first n natural numbers is = n+1/2
Java program of mean of first n natural numbers
Now we write a java program to find mean of first n natural numbers . In this program we take input from user then calculate mean of given n natural numbers as .
import java.util.Scanner;
public class MeansFirstNaturalNumber {
public static void main(String[] args) {
double n ;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first nutural number value:");
n = sc.nextFloat();
//calculate average of n natural number
double average = (n+1)/2;
System.out.println("Means of first natural number "+ n+" is "+ average);
sc.close();
}
}
Output:
Enter the first nutural number value:
50
Means of first natural number 50.0 is 25.5
In this tutorial we have learned how to calculate mean of first n natural numbers and then we have written java program to find mean of first n natural numbers . You can learn more java program for coding .