In: Computer Science
Create a console application named TakeHomePay.cs under your homework directory (please refer to the earlier recorded Zoom sessions if you are not sure how to do it) that calculates the take-home pay for an employee. The two types of employees are salaried and hourly. Allow the user to input the employee’s first and last name, id, and type. If an employee is salaried, allow the user to input the salary amount. If an employee is hourly, allow the user to input the hourly rate and the number of hours clocked for the week. For hourly employees, overtime is paid for hours over 40 at a rate of 1.5 of the base rate. For all employees’ take-home pay, federal tax of 18% is deducted. A retirement contribution of 10% and a Social Security tax rate of 6% should also be deducted. Use appropriate constants and decision making structures.
C# program to calculate take home pay of an employee based on their type
Code:
using System;
class HelloWorld {
//defining constants for deductions
public const double federal_tax=0.18, retirement=0.10, social_security=0.06;
//main method
static void Main() {
//declaring variables
string first_name, last_name, id, type;
string user_input;
double salary=0, hourly_rate, take_home_pay=0;
int hours_worked;
//reading first name, last name, ID, and type
Console.Write("Enter First Name: ");
first_name=Console.ReadLine();
Console.Write("Enter Last Name: ");
last_name=Console.ReadLine();
Console.Write("Enter ID: ");
id=Console.ReadLine();
Console.Write("Enter Type: ");
type=Console.ReadLine();
//checking if type is salaried
if(type=="salaried"){
//if yes, reading Salary Amount
Console.Write("Enter Salary Amount: ");
user_input=Console.ReadLine();
salary=Convert.ToDouble(user_input);
}
//checking if type is hourly
else if(type=="hourly"){
//if yes, reading hourly rate and number of hours worked
Console.Write("Enter Hourly Rate: ");
user_input=Console.ReadLine();
hourly_rate=Convert.ToDouble(user_input);
Console.Write("Enter Number of Hours Worked: ");
user_input=Console.ReadLine();
hours_worked=Convert.ToInt32(user_input);
//checking if hours worked is greater than 40
if(hours_worked<=40){
//if not, calculating salary
salary=hourly_rate*hours_worked;
}
//if yes
else{
//calculating salary for first 40 hours
salary=40*hourly_rate;
//calculating overtime pay for the remaining hours
double overtime=(hours_worked-40)*(1.5*hourly_rate);
//calculating total salary
salary+=overtime;
}
}
//calculating take home pay after the deductions
take_home_pay=salary-(salary*federal_tax)-(salary*retirement)-(salary*social_security);
//printing take home pay
Console.WriteLine("The Take Home Pay is: "+take_home_pay);
}
}
Code Screenshot:
Output:
1) Salaried Employee:
2) Hourly Employee: