I don't understand if the calculation for netPay = grossPay - (fedtax withholding + social security tax withholding). Are my calculations correct within the program? Dealing with such in editedTax?? If someone could help I'd appreciate it.
More info: When I display netPay within an output, I receive a runtime error, where I couldn't convert the negative to currency with {0:c2}.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
string empName;
string userInput;
double netPay;
double editedTax1;
double grossPay;
double editedTax2;
double hrsWorked;
double ovtWorked;
double payRate;
const double FED_TAX = .28;
const double SS_TAX = 7.65;
// step 1
Console.WriteLine(" WEEKLY PAYROLL INFORMATION");
// step 2
Console.WriteLine(" --------------------------");
// step 3
Console.Write("\n Please enter the employer's name: ");
empName = Console.ReadLine();
//step 4
Console.Write("\n Please enter the number of hours worked this week: ");
userInput = Console.ReadLine();
hrsWorked = Convert.ToDouble(userInput);
// step 5
Console.Write("\n Please enter the number of OVERTIME HOURS worked this week: ");
userInput = Console.ReadLine();
ovtWorked = Convert.ToInt32(userInput);
// step 6
Console.Write("\n Please enter employee's HOURLY PAY RATE: ");
userInput = Console.ReadLine();
payRate = Convert.ToDouble(userInput);
// step 7
grossPay = (hrsWorked * payRate + ovtWorked * 1.5 * payRate);
// step 8
editedTax1 = FED_TAX * grossPay;
// step 9
editedTax2 = SS_TAX * grossPay;
// step 10
netPay = editedTax1 + editedTax2 - grossPay;
// step 11
Console.WriteLine("\n\n The weekly payroll information summary for: " + empName);
Console.WriteLine("\n Gross pay: {0:C2} ", grossPay);
// step 12
Console.WriteLine(" Federal income taxes witheld: {0:C2} ", editedTax1);
Console.WriteLine(" Social Security taxes witheld: {0:C2} ", editedTax2);
Console.WriteLine(" Net Pay: {0:C2}", netPay);
}
}
}
netPay is assigned the opposite value in the code as compared to your description below.
I don't see any syntax errors or anything.
What is the problem you're having? What are some of the things you've tried?
The reason you're getting a negative number in the calculation is because your SS_TAX is 7.65. I think the number you want is 0.0765.
I'm not sure exactly what you're asking for, but simple substitution in the assignment statements yields the following formulas:
Since
editedTax1 = FED_TAX * grossPay;
editedTax2 = SS_TAX * grossPay;
netPay = editedTax1 + editedTax2 - grossPay;
then
netPay = FED_TAX * grossPay + SS_TAX * grossPay - grossPay;
meaning
netPay = grossPay * (FED_TAX + SS_TAX - 1);
so something seems a little off here...
Are you sure you don't want
netPay = grossPay - (editedTax1 + editedTax2);
instead of
netPay = editedTax1 + editedTax2 - grossPay;
This seems to match what you're looking for as
netPay = grossPay - (FED_TAX * grossPay + SS_TAX * grossPay);
or
netPay = grossPay * (1 - (FED_TAX + SS_TAX));
...unless I'm missing something, of course.
Edit: I was missing something. Your tax constants are a percent, but you're not dividing by 100 when you do calculations with them. You have two options:
Divide by 100 when you use the values in a calculation, like:
editedTax1 = (FED_TAX / 100) * grossPay;
Store the constants as the decimal representation, not a percent, like:
const double FED_TAX = .0028;
I have tested this code on my machine and it works correctly:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
const double FEDERAL_TAX_RATE= 0.28;
const double SOCIAL_SECURITY_RATE = 0.0765; // I am assuming the 7.65 was supposed to be 7.65%... therefore it should be 0.0765
Console.WriteLine(" WEEKLY PAYROLL INFORMATION");
Console.WriteLine(" --------------------------");
Console.Write("\n Please enter the employer's name: ");
string empName = Console.ReadLine();
Console.Write("\n Please enter the number of hours worked this week: ");
double hrsWorked = Convert.ToDouble(Console.ReadLine());
Console.Write("\n Please enter the number of OVERTIME HOURS worked this week: ");
double ovtWorked = Convert.ToInt32(Console.ReadLine());
Console.Write("\n Please enter employee's HOURLY PAY RATE: ");
double payRate = Convert.ToDouble(Console.ReadLine());
double grossPay = CalculateGrossPay(hrsWorked, payRate, ovtWorked);
double federalTaxWithheld = CalculateTax(grossPay, FEDERAL_TAX_RATE);
double socialSecurityWithheld = CalculateTax(grossPay, SOCIAL_SECURITY_RATE);
double netPay = CalculateNetPay(grossPay, federalTaxWithheld + socialSecurityWithheld);
Console.WriteLine("\n\n The weekly payroll information summary for: " + empName);
Console.WriteLine("\n Gross pay: {0:C2} ", grossPay);
Console.WriteLine(" Federal income taxes witheld: {0:C2} ", federalTaxWithheld);
Console.WriteLine(" Social Security taxes witheld: {0:C2} ", socialSecurityWithheld);
Console.WriteLine(" Net Pay: {0:C2}", netPay);
Console.ReadLine(); // You don't need this line if your running from the command line to begin with
}
static double CalculateGrossPay(double HoursWorked, double PayRate, double OvertimeHoursWorked)
{
return PayRate * (HoursWorked + 1.5 * OvertimeHoursWorked);
}
static double CalculateTax(double GrossPay, double TaxRate)
{
return GrossPay * TaxRate;
}
static double CalculateNetPay(double GrossPay, double TaxAmount)
{
return GrossPay - TaxAmount;
}
}
}
Since it looks as though this is your first programming course, I'll offer you some pointers that they probably will not emphasize in your class:
Use descriptive variable names. If you notice yourself putting a number after your variable name, it can probably done a different and more readable way!
Utilize functions, they bundle common tasks in one section of code and increase readability significantly.
You may want to add some exception handling or validation to this code. For example, what if you accidentally passed -1 into OverTimeHours?
I understand this might not matter at this point in your programming journey, but it's always good to start off using coding techniques that make your code more readable and less confusing, especially for people who may have to maintain it in the future.
Related
I am not able to bring my final output in decimal terms
When i tried to convert double to decimal it gives error
using System;
namespace Recurring_Deposit_Calc
{
class Program
{
private double _amount, _month,_a;
private double _b,_simpleintrest,_matureAmount,_x;
public void Calulate() {
Console.WriteLine("Intrest Rate :6.8%");
Console.WriteLine("Enter amount you deposit per month:");
_amount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter months:");
_month = Convert.ToInt32(Console.ReadLine());
//Calculting Simple Intrest
_simpleintrest = _amount * _month*_a/ 2 * 12*_b;
_a = _month + 1;
_b = 7.65/100;
//Calculating Maturity Amount
_x = _amount * _month;
_matureAmount = _x + _simpleintrest;
Console.WriteLine("Amount is :{0}",_matureAmount);
}
}
Code link
Output
If you want convert to decimal, you can use the Convert.ToDecimal method.
Console.WriteLine("Amount is :{0}",_matureAmount.ToString("0.00"));
If you want upto 1 decimal place, use .ToString("0.0").
I got the output as expected
New problem how to reduce the place value of final answer
private double p, r = 6.8, i, totalDeposit, maturityAmount;
private int n;
public static void Main(string[] args){
Console.WriteLine("Interest Rate :6.8%");
Console.WriteLine("Please enter per month deposit amount:");
p = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter months:");
n = Convert.ToInt32(Console.ReadLine());
//recurring deposit simple interest formula
//i=p*(n(n+1)/2*12)*r/100
i = p * (n * (n + 1) * r / 2400);
totalDeposit = p * n;
maturityAmount = totalDeposit + i;
Console.WriteLine("Amount of maturity = " +
"Totoal money deposited+Interest:{0}+{1}={2}", totalDeposit, i, maturityAmount);
}
Output
******Recurring Deposit Calculator******
Interest Rate :6.8%
Please enter per month deposit amount:
4567
Enter months:
7
Amount of maturity = Totoal money
deposited+Interest:31969+724.6306666666667=32693.630666666668
I'm having a problem with the last line for "deduction".
it's a program to calculate an employee's net pay. every output is coming up as the teacher wants except for the deductions. when ran it's showing "292.22" instead of "368.72" with input being: hours = 48.55, rate = 25.50, and dependents = 3.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Program5
{
class NetPay
{
static double hours, rate, dependents;
const double TAXRATE = .23;
const double DEPENDENTS = 25.50;
static void Main(string[] args)
{
GetInput();
Calculations();
}
static void GetInput()
{
Console.Write("Enter hours worked: ");
hours = double.Parse(Console.ReadLine());
Console.Write("Enter hourly rate: ");
rate = double.Parse(Console.ReadLine());
Console.Write("Enter number of dependents: ");
dependents = double.Parse(Console.ReadLine());
}
static void Calculations()
{
double gross, netPay, deduction;
Console.WriteLine("\nHourse Worked:\t\t\t\t\t {0:c}", hours);
Console.WriteLine("Hourly Rate:\t\t\t\t\t {0:c}", rate);
Console.WriteLine("Dependents:\t\t\t\t\t\t{0}", dependents);
if (hours > 40)
gross = Math.Round((((hours - 40) * 1.5 ) * rate) + (40 * rate), 2);
else
gross = Math.Round(hours * rate, 2);
Console.WriteLine("Gross Pay:\t\t\t\t\t{0:c}", gross);
dependents = Math.Round(dependents * DEPENDENTS, 2);
gross = gross - dependents;
deduction = Math.Round(gross * TAXRATE, 2);
Console.WriteLine("Deductions:\t\t\t\t\t{0:c}", deduction);
Console.WriteLine("\t\t\t\t\t\t---------");
netPay = gross - deduction;
Console.WriteLine("Net Pay:\t\t\t\t\t{0:c}", netPay);
}
}
}
There is my program.
static void Main(string[] args)
{
double weeklySales = 0, grossPay = 0, fedTax = 0, socSecurity = 0, retirement = 0, totDeductions = 0, takeHomePay = 0;
Console.WriteLine("Please enter your total for sales for the week.");
weeklySales = Convert.ToDouble(Console.ReadLine());
grossPay = weeklySales * .07;
fedTax = grossPay * .18;
socSecurity = grossPay * .06;
retirement = grossPay * .1;
totDeductions = fedTax + socSecurity + retirement;
takeHomePay = grossPay - totDeductions;
Console.WriteLine("Your total sales for the week were $ ", weeklySales);
Console.WriteLine("Your gross pay for the week was $ ", grossPay);
Console.WriteLine("Your Federal Taxes for the week were $ ", fedTax);
Console.WriteLine("You were deducted $ ", socSecurity, " for social security.");
Console.WriteLine("Your retirement contribution was $ ", retirement);
Console.WriteLine("The total amount of of deductions were $ ", totDeductions);
Console.WriteLine("Your take home pay for the week is $ ", takeHomePay);
Console.ReadLine();
}
The problem is an incorrect output.
Please enter your total for sales for the week.
123
Your total sales for the week were $
Your gross pay for the week was $
Your Federal Taxes for the week were $
You were deducted $
Your retirement contribution was $
The total amount of of deductions were $
Your take home pay for the week is $
I need to include calculated values to output.
How can I do this?
Also, this line won't work:
Console.WriteLine("You were deducted ${0}", socSecurity, " for social security.");
It will need to be:
Console.WriteLine("You were deducted ${0} for social security.", socSecurity);
The other alternative is to use string concatenation:
Console.WriteLine("You were deducted $" + socSecurity + " for social security.");
If I understood your problem correctly, you would just have to replace $ with {0} to show the actual values.
In the current code, you're not specifying where the values should be entered into the string outputs. You can change them as follows and receive the output that you're looking for:
Console.WriteLine("Your total sales for the week were ${0}", weeklySales);
Console.WriteLine("Your gross pay for the week was ${0}", grossPay);
Console.WriteLine("Your Federal Taxes for the week were ${0}", fedTax);
Console.WriteLine("You were deducted ${0} for social security.", socSecurity);
Console.WriteLine("Your retirement contribution was ${0}", retirement);
Console.WriteLine("The total amount of of deductions were ${0}", totDeductions);
Console.WriteLine("Your take home pay for the week is ${0}", takeHomePay);
I would like to make a program that calculates circumference. Though, would much rather have a rounded answer (two decimal places) as a pose to seven decimal places. But when I use the Math.Round method, it doesn't seem to round. What am I doing wrong?
Console.Write("Circumference Calculator! Enter the radius of the circle: ");
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + ans + " squared.");
Math.Round does not modify provided argument - it returns a new value. So you have to assigned it back to a variable to make it work:
ans = Math.Round(ans, 2);
You have to use the return value of Math.Round, it doesn't take the variable by reference.
//Greet user & give instructions
#region
Console.WriteLine("Circumference Calculator");
Console.WriteLine("");
Console.Write("Welcome to the Circumference Calculator! Please enter the radius of the circle: ");
#endregion
//Get input from user and calculate answer
#region
Console.ForegroundColor = oldColour;
int inputRadius = Convert.ToInt32(Console.ReadLine());
double ans = Math.Sqrt(inputRadius) * Math.PI;
Console.ForegroundColor = ConsoleColor.DarkYellow;
double roundedAnswer = Math.Round(ans, 2);
Console.WriteLine("Your answer is: " + roundedAnswer + " squared.");
#endregion
I want to create a C# application that inputs one salesperson's item sold for the last week and calculates and displays that salesperson's earnings. The sales person receives $200 per week plus 9% of their gross sales for that week. Having been supplied a list of items sold by each salesperson and their values, how do I convert the user input to int? Everything else works fine.
double value1, value2, value3, value4;
value1 = 239.99;
value2 = 129.75;
value3 = 99.95;
value4 = 350.89;
Console.Write("Enter number sold of product #1: ");
int item1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #2: ");
int item2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #3: ");
int item3 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter number sold of product #4: ");
int item4 = Convert.ToInt32(Console.ReadLine());
double sales1, sales2, sales3, sales4, totalSales;
sales1 = item1 * value1;
sales2 = item2 * value2;
sales3 = item3 * value3;
sales4 = item4 * value4;
totalSales = sales1 + sales2 + sales3 + sales4;
double commission;
int weeklyPay = 200;
commission = (9/100) * totalSales;
double salary;
salary = commission + weeklyPay;
Console.Write("Earnings this week: " + salary);
Console.ReadLine();
Although your approach seems correct at first sight, you should cast strings into simple datatypes in a non-vexing style.
Check this (quite verbose) code:
string userInput = Console.ReadLine();
int parsedValue;
if(!Int32.TryParse(userInput, out parsedValue))
{
// Decide what to do here, parsing failed!
}
else
{
// String format from user was valid and parsed value is in parsedValue variable.
}
If your are interested in a more detailed argument of why this is better coding, check Eric Lippert article about Vexing Exceptions.
Earnings this week will always return 200. This is not because the user input is not being read (see jih's answer for a more robust method of parsing a string to an int) but because of a bug:
commission = (9/100) * totalSales;
The problem is that you are doing integer division.
Try
commission = 0.09 * totalSales;
or
commission = 9.0/100.0 * totalSales;
The parentheses around 9/100 are redundant but are ok if they improve readability.
try this
commission = ((double)9/100) * totalSales;