Java program to find average of array element

Java program to find average of array number

Program:-

Method 1
Java program to fined average of array element using enhanced for loop

import java.util.Scanner;
class group{
public static void main(String arng[]){
int value[]= new int[5];
Scanner data = new Scanner(System.in);
float sum =0;
System.out.println("Enter 5 element of array" );
// Enhanced for loop
for(int i : value)
{
i = data.nextInt();
// sum all element
sum = sum+i;
}
System.out.println("Average of array element");
// divide by total number
sum = sum/value.length;
System.out.println(sum);
}
}

Output:-

Enter 5 element of array
5
6
7
8
9
Avarge of array element
7.0

Output:-

Enter 5 element of array
5
21
28
24
25
Average of array element
20.6


Method 2
Java program to fined average of array element using simple way

import java.util.Scanner;
class group{
public static void main(String arng[]){
int value[]= new int[5];
Scanner data = new Scanner(System.in);
float sum =0;
int i;
System.out.println("Enter 5 element of array" );
// Enter 5 number
for(i = 0; i < 5; i++)
value[i] = data.nextInt();
// sum all element
for(i = 0; i < 5; i++)
sum = sum+value[i];
System.out.println("Average  of array element");
// divide by total number
sum = sum/value.length;
System.out.println(sum);
}
}

Output:-

Enter 5 element of array
25
29
35
75
79
Average of array element
48.6



Learn Java:-
Java codes

Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top