# Java Program of Find Average Age

#### Question:

One of the least Insurance agencies recruited employees for their collection department. Now the HR needs a report as the average age of all the employees working in that department. Write a code to calculate the average age.

Implement a method “calculateAverage(int[] age)” to calculate the average age and return the result to the caller function.

Note:

1. Age limit should be minimum of 28 years and maximum of 40 years.
2. Minimum of 2 employees is mandatory to calculate average age, if fails, the output should be “Please enter a valid employee count”
3. If any of the age is invalid, terminate the process and display “Invalid age encountered!”

Refer the sample given for read and display the output.

Sample Input 1:

Enter total no.of employees:

3

Enter the age for 3 employees:

30

31

32

Sample Output 1:

The average age is 31.00

Sample Input 2:

Enter total no.of employees:

2

Enter the age for 2 employees:

29

36

Sample Output 2:

The average age is 32.50

Sample Input 3:

Enter total no.of employees:

1

Sample Output 3:

Please enter a valid employee count

#### Test.java

```import java.util.*;
import java.text.DecimalFormat;
public class Test
{

double calculateAverage(int[] age)
{
int len=age.length;
double sum=0.0;
for(int i=0;i<len;i++)
{
sum+=age[i];
}
double avg=sum/len;
return avg;
}

public static void main (String[] args) {
Scanner sc =new Scanner(System.in);
Test obj=new Test();

System.out.println("Enter total no.of employees:");
int n=sc.nextInt();
int flag=0;
if(n>1)
{ int[] age=new int[n];
System.out.println("Enter the age for "+n+" employees:");

for(int i=0;i<n;i++)
{
int temp=sc.nextInt();
if(temp>=28 && temp<=40)
{
age[i]=temp;
}
else
{
System.out.println("Invalid age encountered!");
flag++;
break;
}
}

if(flag==0)
{   DecimalFormat df=new DecimalFormat("####.00");
System.out.println("The average age is "+df.format(obj.calculateAverage(age)));
}
}
else
{
System.out.println("Please enter a valid employee count");
}
}
}```