Using a while loop create a program that calculates the monthly salary of N employees[where N is the number of employees that's entered by the user]. If the employee worked less or equal to 160 hours, the employee will be paid N$240.00 per hour. For any hours greater than 160, the employee is paid N$320.00 per hour.
SOLUTION CODE FOR THE ABOVE QUESTION
package com.company;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of Employees: ");
int number_of_employees = sc.nextInt();
if(number_of_employees == 0)
{
System.out.println("No employees given to our program");
}
else if(number_of_employees < 0)
{
System.out.println("Number of Employees cannot be negative");
}
else
{
//Declare an array to store the hours worked by every employee
int [] hours_worked = new int[number_of_employees];
int number_of_employee_count = 0;
while(number_of_employee_count < number_of_employees)
{
System.out.print("Enter hours worked by Employee "+(number_of_employee_count+1)+": ");
int hours = sc.nextInt();
hours_worked[number_of_employee_count] = hours;
number_of_employee_count = number_of_employee_count+1;
}
//Now lets calculate salary for the Employees
System.out.println("");
int stop_condition_count = 0;
while(stop_condition_count < number_of_employees)
{
//check if the number of hours are more than 160
if(hours_worked[stop_condition_count]>160)
{
int monthly_salary = hours_worked[stop_condition_count]*320;
System.out.println("Monthly salary for Employee "+(stop_condition_count+1)+" = N$"+monthly_salary);
}
else
{
int monthly_salary = hours_worked[stop_condition_count]*240;
System.out.println("Monthly salary for Employee "+(stop_condition_count+1)+" = N$"+monthly_salary);
}
stop_condition_count = stop_condition_count + 1;
}
}
}
}
SAMPLE PROGRAM OUTPUT FOR THE ABOVE
Comments
Leave a comment