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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.