Average of N Numbers in Java using For loop | While loop | Array
Average of N numbers in java using For loop, While loop, and Array.
In this article, you will learn how to find the average of N numbers in java using For loop, While loop, and Array.
Example
Enter the number of elements to calculate the average::
3
Enter 3 elements one by one
34563
3522
6553
The average of the entered input numbers is = 14879.333
You should have knowledge of the following topics in Java programming to understand this program:
- Java
java.util.Scannerpackage - Java
main()method - Java
System.out.println()function - Java
Forloop - Java
Whileloop - Java
Array
Source Code
// Average of N Numbers in Java using For loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println ("Enter the number of elements to calculate the average::");
int x=in.nextInt(); // Counting of input elements
float avg=0; // Average of the input elements
System.out.println ("\nEnter " + x + " elements one by one");
int y=0;
for(int i = 0; i < x; i++) {
y=in.nextInt();
avg += y;
}
avg /= x;
System.out.println ("\nThe average of the entered input numbers is = " + avg);
}
}
Output
Enter the number of elements to calculate the average::
3
Enter 3 elements one by one
34563
3522
6553
The average of the entered input numbers is = 14879.333
Explanation
In this given program, we have taken the inputs size of the input elements 3 and these input elements 34563, 3522, 6553 to calculate the average of these elements.
After that, I made the sum of each element of the array we divided this value by the input value 3.
After the whole calculation, It will return the 14879.333 the output of the program.
Average of N Numbers in Java using While loop
// Average of N Numbers in Java using While loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println ("Enter the number of elements to calculate the average::");
int x=in.nextInt(); // Counting of input elements
float avg=0; // Average of the input elements
System.out.println ("\nEnter " + x + " elements one by one");
int y=0, i=0;
while (i < x) {
y=in.nextInt();
avg += y;
i++;
}
avg /= x;
System.out.println ("\nThe average of the entered input numbers is = " + avg);
}
}
Average of N Numbers in Java using Array
// Average of N Numbers in Java using Array
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println ("Enter the number of elements to calculate the average::");
int x=in.nextInt(); // Counting of input elements
float avg=0; // Average of the input elements
System.out.println ("\nEnter " + x + " elements one by one");
int arr[] = new int[x]; // Array initialized with length
// It will read the inputs and puts them into an array variable
// It will make a sum of each element of the array
for(int i=0; i<x; i++) {
arr[i]=in.nextInt();
avg += arr[i];
}
avg/=x;
// It will print the final output
System.out.println ("\nThe average of the entered input numbers is = " + avg);
}
}
Output
Enter the number of elements to calculate the average::
4
Enter 4 elements one by one
34562
35223
65534
45685
The average of the entered input numbers is = 45251.0