0% found this document useful (0 votes)
29 views1 page

26.find Average Age

The document contains Java code to calculate the average age of employees entered by the user if their ages are between 28 and 40, otherwise it returns an error message. It takes the number of employees as input, stores their ages in an array, calculates the sum of ages between 28-40 and divides by total employees to get the average, otherwise it returns an error.

Uploaded by

VIJAY V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views1 page

26.find Average Age

The document contains Java code to calculate the average age of employees entered by the user if their ages are between 28 and 40, otherwise it returns an error message. It takes the number of employees as input, stores their ages in an array, calculates the sum of ages between 28-40 and divides by total employees to get the average, otherwise it returns an error.

Uploaded by

VIJAY V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

*;

class Test
{
public static void main(String args[])
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter total no. of employees: ");
int noofemployees = scn.nextInt();

if(noofemployees>1)
{
double temp = 0.0;
int i=0,avarage = 0,count=0;
int[] arr = new int[noofemployees];

System.out.println("Enter the age for "+noofemployees+" employees:");


for(i = 0; i<noofemployees; i++)
{
arr[i]=scn.nextInt();

if(arr[i]>=28 && arr[i]<=40)


{
temp= temp+arr[i];
}
else if(arr[i]<28 || arr[i]>40)
{
count++;
break;
}
}
if(count!=1)
{
temp = temp/noofemployees;
System.out.println("The average age is
"+String.format("%.02f",temp));
}
else
{
System.out.println("Invalid age encountered!");
}
}
else
{
System.out.println("Please enter a valid employee count");
}

}
}

You might also like