Home »
Java Programs »
Java Basic Programs
Java program to find mean of a given number
In this java program, we are going to learn how to read N numbers and find the mean of all input number?
Submitted by IncludeHelp, on November 15, 2017
Given N integer numbers and we have to find mean of all numbers using java program.
Example 1:
Input:
Entered numbers: 63, 25, 45
Output:
Mean of the given numbers is : 44
Program to find mean of given numbers in java
import java.util.Scanner;
public class CalculateMean
{
public static void main(String args[])
{
// initialize and declaring the objects.
int n, i, sum=0, mean;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
// enter number you have to enter.
System.out.print("How many Number you want to Enter : ");
n = scan.nextInt();
// enter the numbers.
System.out.println("Enter " +n+ " Numbers : ");
// this will access all numbers and make sum of the numbers.
for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
sum = sum + arr[i];
}
// fomula to calculate mean.
mean = sum/n;
System.out.print("Mean of the given numbers is : " +mean);
}
}
Output
First run:
How many Number you want to Enter : 5
Enter 5 Numbers :
25
36
95
52
58
Mean of the given numbers is : 53
Second run:
How many Number you want to Enter : 3
Enter 3 Numbers :
63
25
45
Mean of the given numbers is : 44
Java Basic Programs »