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

Code:

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");
        }
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

Leave a Comment

Your email address will not be published. Required fields are marked *