I'm trying to calculate salaries and have a problem determining the tax as follows.
The first 60,000 dollars gross salary is not taxed. On the part of the gross salary in between 60,000 dollars and 100,000 dollars, paid 20% taxes while on a part of the gross salary over 100,000 dollars pays tax of 25%. Write a program that calculates the net salary (salary after tax deduction) for the entered gross salary (salary before tax).
In the first if I set two conditions. That the salary is less than the first limit and must not be equal to zero or less than zero. Under this condition, the net salary should be equal to the gross salary. However the net salary is always higher than the gross salary while the NOT operator is there.
When I delete the NOT operator then everything is fine. Problem is why the NOT operator increases the value of my net salary?
Here is the code
const double s0 = 0;// s0, s1 and s2 are tax percentages
const double s1 = 0.2;
const double s2 = 0.25;
const double border1= 60000;
const double border2 = 100000;
double neto_salary;
Console.WriteLine(" Enter your bruto sallary ");
double bruto_salary = double.Parse(Console.ReadLine());
if ( (bruto_salary<border1) && (bruto_salary !<=0) ) //NOT operator on this line
{
neto_salary = bruto_salary * (1 - s0);
Console.WriteLine(" Your salary is {0} $ ", neto_salary);
}
else if ((bruto_salary>border1) && (bruto_salary<border2) )
{
neto_salary = border1 + (bruto_salary - border1) * (1 - s1);
Console.WriteLine(" Your salary is {0} $ ", neto_salary);
}
else
{
neto_salary = border1 + (border2 - border1) * (1 - s1) + (bruto_salary - border2) * (1 - s2);
Console.WriteLine(" Your salary is {0} $ ", neto_salary);
}
I don't think that:
bruto_salary !<= 0 // salary not less than or equal to zero.
is valid C# syntax, the way to do that would be:
! (bruto_salary <= 0) // not (salary less than or equal to zero).
But, since that's equivalent to:
bruto_salary > 0 // salary greater than zero.
I'd opt for that instead.
As an aside, I suspect the expression bruto_salary !<=0 is actually being treated as bruto_salary! <= 0, where ! is the postfix null-forgiving operator, introduced in C# 8.
In a run-time context(a), x! just evaluates as x so your statement really means the totally opposite sense from what you wanted:
bruto_salary <= 0
(a) Nullable checking is a compile time thing that performs static analysis on your code to discover certain problems. See here for more detail.
Related
I'm making a library program that asks for users to input the amount of books checked out and the amount of days they are over due. If its under or equal to 7 days they are charge 10 cents for each book over due after 7 days its 20 cents for each book. We are supposed to use more than one method and I get two errors:
Use of unassigned local variable 'totalCharge'
There is no argument given that corresponds to the required formal parameter 'daysOverdue' of Program.charge(double,double,double)'
I think I know what the first error means but I thought I already declared it a variable in the first line.
Here's the code so far:
static void Main(string[] args){
double totalCharge;
Console.WriteLine("Please enter the number of books checked out.");
double booksChecked = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of days they are
overdue.");
double daysOverdue = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your total charge for {0} days overdue is {1}.",
daysOverdue, totalCharge.ToString("C"));
Console.ReadKey();
totalCharge = charge();
}
private static double charge (double daysOverdue, double booksChecked,
double totalCharge)
{
if (daysOverdue <= 7)
{
return totalCharge = booksChecked * daysOverdue * .10;
}
else
{
return (booksChecked * .70) + (booksChecked) * (daysOverdue - 7)
* (.20);
}
}
}
}
Your code has a number of problems, which I'll review here. My corrections are at the end of this answer. I recommend putting your code and mine side by side and reviewing the differences carefully.
First, you cannot read the value out of a variable before you have assigned a value. You must assign something to it first.
You need to call charge(...) before printing out the value of totalCharge.
Second, you don't need to pass the value of totalCharge to your charge(...) method: it returns the total charge! So remove that parameter entirely.
Third, you need to pass parameters to the charge method.
Fourth, you had some formatting problems. Please review my code to see how I've formatted my code differently. If a line of code is continued onto the next line, use indentation to reflect this. According to C# conventions, function names should be capitalized.
Lastly, this isn't necessarily a problem, but it doesn't look 'right': in two places, you are assigning Convert.ToInt32(...) to a double. Why? Those should be integers.
static void Main(string[] args)
{
Console.WriteLine("Please enter the number of books checked out.");
double booksChecked = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the number of days they are overdue.");
double daysOverdue = Convert.ToInt32(Console.ReadLine());
// assign before printing out value
// pass the two parameters into the function
double totalCharge = Charge(daysOverdue, booksChecked);
Console.WriteLine("Your total charge for {0} days overdue is {1:C}.",
daysOverdue,
totalCharge);
Console.ReadKey();
}
private static double Charge(double daysOverdue, double booksChecked)
{
if (daysOverdue <= 7)
{
return booksChecked * daysOverdue * .10;
}
else
{
return (booksChecked * .70) + booksChecked * (daysOverdue - 7) * (.20);
}
}
I need some clarification. I'm doing a lab project that wants me to gather 5 numbers, then when a button is clicked it adds them, divides them into dozens, and also tells the remainder. here's a portion of my code, I'm wanting to know if there's something I'm missing in the line with label2?
int sum = num1 + num2 + num3 + num4 + num5;
int dozen = sum / 12;
int remainder = sum % 12;
label2.Text = "The total is {0}. That's {1} dozen with {2} remainders",sum , dozen, remainder;
}
}
}
any help would be appreciated.
Yes, you're missing a call to string.format or nowadays you can do:
label2.Text = $"The total is {sum}. That's {dozen} dozen with {remainder} remainders";
Change to this:
label2.Text = String.Format("The total is {0}. That's {1} dozen with {2} remainders",sum , dozen, remainder);
String.Format take arguments.
I'm very new to programming and I wanted to create a small console app that calculates Force when given 2 charges and the distance between them according to Coulomb's Law.
I seem to be having a problem with converting strings to decimals.
I'm not quite sure how to multiply multiple stings( Q1, Q2 and r)
This is an image of my code along with the console display and error message.
Console.WriteLine("Welcome to Dieter's Coulomb's Law Calculator!");
Console.WriteLine("Press Enter to continue.");
String Q1;
String Q2;
String r;
Decimal k = (9 * 10 ^ 9);
Console.ReadLine();
Console.WriteLine("All you need to do is insert the first charge, the second chage and the distance between them.");
Console.WriteLine("Remember to give your values in coulombs and meters.");
Console.WriteLine("Charge 1: ");
Q1 = (Console.ReadLine());
Console.WriteLine("Charge 2: ");
Q2 = Convert.ToString(Console.ReadLine());
Console.WriteLine("Distance in m: ");
r = Convert.ToString(Console.ReadLine());
Console.WriteLine(" Equation: k*|Q1*Q2|/r^2");
Console.WriteLine("Equation: " + "9*10^9*" + "|" + Q1 + "*" + Q2 + "|" + "/" + r + "^2" );
Decimal ans = ( k * Convert.ToDecimal(Q1) * Convert.ToDecimal(Q2) / Convert.ToDecimal(r) * Convert.ToDecimal(r));
Console.WriteLine("Answer: " + ans);
Console.ReadLine();
As already pointed out in the comments by Nattrass +5*10^-3 is not a valid input for Convert.ToDecimal().
I assume you want the user to enter the numbers in exponential notation.
So I suggest to change your console input from +5*10^-3 to 5E-3 and use the following snippet to parse your decimal using NumberStyles:
using System.Globalization;
...
// with any you did not have to care about decimal points and thousand sign
// leading sign and so on..
decimal Q1 = Decimal.Parse("5E-3", NumberStyles.Any); // Q1 = 0.005
// or explicit:
decimal Q2 = Decimal.Parse("-1E-1", NumberStyles.AllowExponent
| NumberStyles.AllowDecimalPoint
| NumberStyles.AllowLeadingSign); // Q2 = -0,1
But we have to be careful with the decimal point (dot or comma) so you can just add CultureInfo.InvariantCulture to the parse method.
decimal Q2 = Decimal.Parse( "-1E-1", NumberStyles.AllowExponent
| NumberStyles.AllowDecimalPoint
| NumberStyles.AllowLeadingSign,
CultureInfo.InvariantCulture);
Your code has also some other issues.
For example calculating your constant Decimal k = (9 * 10 ^ 9);
You are using the ^ operator to indicate the power of 9 by a base of ten but in C# it is used as XOR operator. So you should use the Math.Pow() to calculate your constant.
decimal k = (decimal)(9 * Math.Pow(10,9));
You should also avoid Convert.ToString(Console.ReadLine());, because the return value is already a type of string.
You can instead try to cast the input from your console to decimal and avoid the multiple casting in the later code, when calculating. This link may help you to extend your code: How to Read decimal numbers from a user in C#?
I am trying to write an extension method off a 'decimal' that calculates the value of a given number in relation to a given lower and upper range. I know that sounds a bit strange so here is a concrete example.
Lets say that there is a pay grade system in place at a particular place of work and the pay rate categories are as follows..
Cat1 (0 - 20 hours) = $20/hour
Cat2 (20 - 30 hours) = $22/hour
Cat3 (30 - 40 hours) = $24/hour
Cat4 (40 - 50 hours) = $26/hour
Cat5 (50+ hours) = $28/hour
So...
If a person worked 14 hours in a given week, they would earn (14 * 20)
If a person worked 27 hours in a given week, they would earn (20 * 20) + (7 * 22)
If a person worked 38 hours in a given week, they would earn (20 * 20) + (10 * 22) + (8 * 24)
and so on..
The extension method I have put together looks like this and its purpose is to return a single decimal that tells me if it falls into the given range or not. if it does it tells me how much by. if not it returns zero or the maximum value for that range. To be honest something about it doesnt seem right.
Can anyone verify that this is the correct logic and perhaps if there is anyway to optimise it?
public static decimal ValueBetween(this decimal m, decimal lower, decimal upper = decimal.MaxValue)
{
return (m > lower)
? (m > upper)
? upper - lower
: m - lower
: 0m;
}
Example Usage with Expected Output
((decimal)14).ValueBetween(0, 20) = 14
((decimal)24).ValueBetween(0, 20) = 20
((decimal)24).ValueBetween(20, 30) = 4
((decimal)24).ValueBetween(30, 40) = 0
((decimal)150).ValueBetween(40) = 110
Thanks.
I think what you have is fine, except maybe you could make it a bit more readable:
public static decimal ValueBetween(this decimal m, decimal lower, decimal upper = decimal.MaxValue)
{
if (m < lower) return 0M;
if (m > upper) return upper - lower;
return m - lower;
}
Write code that will:
• Ask the user for their tax code (A or B) and annual salary
• Display the tax the person has to pay for the year. People on tax code A have
to pay 25% of their salary in tax, those on tax code B must pay 30% if their
salary is $45,000 or less, 33% otherwise.
Example:
What is your tax code? A
What is your annual salary? 42560
Tax due $10,640
so my question is how do i do this in Microsoft Visual studio?
I appreciate all the help and comments :)
Start a console project.
Print out with System.Console.Write ("question?"); or System.Console.WriteLine("question?");
Get input with string input = Console.ReadLine();
Convert to required data type using functions like int salary = int.Parse(input)
Process according to the requirement. You will mostly use if to determine which rate to use.
Remember to repeat the question if the input is not correct; eg: not a number, not A or B.
long amount = [get amount];
string taxCode = [get code];
decimal tax =0;
if (taxCode == "A" && amount <= 45000)
tax = 0.25M * amount;
else if (taxCode == "B" && amount <= 45000)
tax = .3M * amount;
else
tax = .33M * amount;
This should do the trick
This should get you started:
float rate;
if(ddl_Code.SelectedValue == "A")
{
rate = .25F;
}
else if(ddl_Code.SelectedValue == "B" && Convert.ToDecimal(tb_Salary.Text) <= 45000)
{
rate = .3F;
}
else
{
rate = .33F;
}
tb_TaxDue.Text = Convert.ToDecimal(tb_Salary.Text) * rate;
This assumes you have a drop down list for Tax Code and two text boxes for Taxes due and salary.