In the previous article, we have seen Java Program to Calculate Tax to be Deducted from Salary
In this article we will see how to calculate average of N numbers by using Java programming language.
Java Program to Calculate Average of N Numbers
To calculate the average of N numbers first we need to add all the N numbers then dividing the sum of N numbers with N.
Average of N numbers= Sum of N numbers / N
Let’s see different ways to find average of N numbers.
Method-1: Java Program to Calculate Average of N Numbers By Using For Loop
Approach:
- Take total how many numbers as input from user.
- Initially declare sum to 0.
- Then using a for loop take one by one input and add it to sum.
- After taking all the inputs and finding the sum find average by dividing sum with total count of numbers.
- Print the result.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc =new Scanner(System.in);
//declaring variable count, number and sum and initializing value to 0
int count,number,sum=0;
//taking how many numbers as user input
System.out.print("Enter total count of numbers : ");//input
count =sc.nextInt();
//taking the numbers input from user by using for loop
System.out.print("Enter numbers :");
for(int i=1;i<=count; i++)
{
//taking input of number from user
number=sc.nextInt();
//adding that number to sum
sum =sum+number;
}
//finding the average
double avg= (double)sum/count;
//printing result
System.out.println("Average of " + count + " Numbers = " + avg);
}
}
Output: Enter total count of numbers : 5 Enter numbers :2 6 3 8 7 Average of 5 Numbers = 5.2
Method-2: Java Program to Calculate Average of N Numbers By Using While Loop
Approach:
- Take total how many numbers as input from user.
- Initially declare sum to 0.
- Then using a while loop take one by one input and add it to sum.
- After taking all the inputs and finding the sum find average by dividing sum with total count of numbers.
- Print the result.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc =new Scanner(System.in);
//declaring variable count, number and sum and initializing value to 0
int count,number,sum=0;
//taking how many numbers as user input
System.out.print("Enter total count of numbers : ");//input
count = sc.nextInt();
int temp=count;
//taking the numbers input from user by using for loop
System.out.print("Enter numbers :");
while(temp != 0)
{
//taking input of number from user
number=sc.nextInt();
//adding that number to sum
sum =sum+number;
temp--;
}
//finding the average
double avg= (double)sum/count;
//printing result
System.out.println("Average of " + temp + " Numbers = " + avg);
}
}
Output: Enter total count of numbers : 5 Enter numbers :2 2 2 2 2 Average of 0 Numbers = 2.0
Method-3: Java Program to Calculate Average of N Numbers By Using User Defined Method
Approach:
- Take total how many numbers as input from user.
- Initially declare sum to 0.
- Then using a while loop take one by one input and add it to sum.
- After taking all the inputs and finding the sum call a user defined method and pass total count of numbers and sum value as parameter.
- Inside method find average by dividing sum with total count of numbers.
- Print the result.
Program:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc =new Scanner(System.in);
//declaring integer variable count and initializing value to 0
int count=0;
//taking how many numbers as user input
System.out.print("Enter total count of numbers : ");//input
count = sc.nextInt();
//declaring integer variable number and sum and initializing both to 0
int number,sum=0;
//assign count value to temp
int temp=count;
//taking the numbers input from user by using for loop
System.out.print("Enter numbers :");
while(temp != 0)
{
//taking input of number from user
number=sc.nextInt();
//adding that number to sum
sum =sum+number;
temp--;
}
//calling a user defined method findAverage()
findAverage(count,sum);
}
public static void findAverage(int count, int sum)
{
//finding the average
double avg= (double)sum/count;
//printing result
System.out.println("Average of " + count + " Numbers = " + avg);
}
}
Output: Enter total count of numbers : 5 Enter numbers :2 6 3 4 1 Average of 5 Numbers = 3.2
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs: