im stuck with this question - c#

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.

Related

Why the operator NOT increase my net salary?

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.

How can I price group?

I am trying to price group. For example: a tutor costs 100 euro per 5 people; 6 people needs 2 tutors so it comes to 200 euro.
Basically I don't know how to go about this, this is what I tried. What I did works perfectly but let's say the user enters a bigger number and I didn't put an if statement how can I fix it? Thanks
int tutorprice;
int students;
Console.WriteLine("How many students");
students = Convert.ToInt32(Console.ReadLine());
if (students <= 1 && students <= 5)
{
tutorprice = 100;
}
else if (students > 5 && students <=10 )
{
tutorprice = 200;
}
You have 2 problems here.
First of all you need to learn basic implementation. For that you can search help other places.
To implement the best calculating function you should look at this topic :
Math Round to always upper integer

Adding user input into equation through a variable possible? (C#)

I apologize if this is a really simple solution...
So I'm trying to get an Ending Balance from 4 inputs, those being
Starting Balance
Number of months that have elapsed
User Specified yearly interest rate
and a optional monthly contribution that the user can put in. This is what I would imagine the equation to be
balance = contribution + balance + (INTEREST_RATE * balance) + (yearly * balance);
Everything is fine until the compiler states that Use of unassigned local variable 'contribution' This really confuses me because at the comment at the top of the code I have stated that contribution will be an int.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void calculateButton_Click(object sender, EventArgs e)
{
// Constant for the monthly interest rate.
const decimal INTEREST_RATE = 0.005m;
// Local variables
decimal balance; // The account balance
int months; // The number of months
int contribution;
int count = 1; // Loop counter, initialized with 1
decimal yearly;
// Get the starting balance.
if (decimal.TryParse(txtStartBalance.Text, out balance))
{
// Get the number of months.
if (int.TryParse(txtMonths.Text, out months))
{
// Get the yearly interest rate
if (decimal.TryParse(txtYearly.Text, out yearly))
{
//Get monthly contribution
if (int.TryParse (txtMonthly.Text, out contribution));
}
// The following loop calculates the ending balance.
while (count <= months)
{
// Add this month's interest to the balance.
balance = contribution + balance + (INTEREST_RATE * balance) + (yearly * balance);
// Display this month's ending balance.
if (rdoEnglish.Checked == false)
lstDetails.Items.Add("ʻO ka pale hope " + "no ka mahina " + count + " ʻO " + balance.ToString("c"));
else
lstDetails.Items.Add("The ending balance for " + "month " + count + " is " + balance.ToString("c"));
// Add one to the loop counter.
count = count + 1;
}
// Display the ending balance.
txtEnding.Text = balance.ToString("c");
Again thank you for taking the time to help me.
This really confuses me because at the comment at the top of the code I have stated that contribution will be an int.
Yes but you have not assigned a value to the variable in at least one path your code can take. (Specifically if int.TryParse (txtMonthly.Text, out contribution)); fails) And then you try to use contribution in the line:
balance = contribution + balance + (INTEREST_RATE * balance) + (yearly * balance);
You need to assign a value to contribution before you use it. Think about what contribution is supposed to represent and assign it to a value accordingly
Try to declare your variables Like below:
decimal.TryParse(txtStartBalance.Text, out decimal balance);
it is better to make a checkbox for the contribution field (txtMonthly) and set its visibility to Checkbox.checked property so if the user wants to set the optional contribution value, has to check the checkbox to write it down.
Also using that ifs in your code is useless until you throw Exceptions when those TryParse methods retrun false and warn the user to try again with right inputs.

Errors in library fee code using multiple methods

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

Is my program calculating wrong? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So in my C# class, I was asked to;
Write a program that computes the amount of money the computer club will receive from proceeds of their granola bar sales project. Allow the user to enter the number of cases sold and the sale price per bar.Each case contains 12 bars; each case is purchased at $5.00 per case from a local vendor.The club is required to give the student government association 10% of their earnings. Display their proceeds formatted with currency. Write appropriate methods for your solution.
I wrote the program but I am pretty sure the math is not adding up (unless I am screwing up the calculations that I am plugging into my calculator.
Here is my code.
using System;
namespace GranolaBars
{
class Granola
{
static void Main(string[] args)
{
// Give user info on program
Console.WriteLine("The Computer Club buys the cases at $5.00 a case");
Console.WriteLine("The studdent government gets 10% of profit");
Console.WriteLine("Total made from sell is number of cases sold minus 10%");
// Declare the variables
int casesSold;
decimal pricePerBar;
decimal profit;
decimal proceeds;
decimal finalOutCome;
// Set the variables values
casesSold = GetCasesSold();
pricePerBar = GetPricePerCase();
profit = GetProfit(casesSold, pricePerBar);
proceeds = GetProceeds(profit);
finalOutCome = GetFinalOutCome(proceeds, profit);
// The output from the program
Console.WriteLine("The amount cases of sold was: {0} ", casesSold);
Console.WriteLine("The price per bar was {0:C}: ", pricePerBar);
Console.WriteLine("The gross profit is: {0:C}: ", profit);
Console.WriteLine("The student government fees are: {0:C} ", proceeds);
Console.WriteLine("The income minus the student government fees is: {0:C} ", finalOutCome);
Console.ReadKey();
}
public static int GetCasesSold()
// Method that gets the total number of cases sold
{
int CSold;
Console.Write("Enter the number of cases sold: ");
CSold = int.Parse(Console.ReadLine());
return CSold;
}
public static decimal GetPricePerCase()
// Method that gets the price per case of garnola bars
{
decimal PerBar;
Console.Write("Enter the price per bar: ");
PerBar = decimal.Parse(Console.ReadLine());
return PerBar;
}
public static decimal GetProfit(int CasesSold, decimal PricePerBar)
// Method to get the Profit
{
decimal PriceofCase = 5.00M;
decimal Earnings;
Earnings = ((PricePerBar * 12) - PriceofCase);
return Earnings;
}
public static decimal GetProceeds(decimal Profit)
// Method to get the Proceeds
{
decimal StudentGovFunds = .10M;
return (Profit * StudentGovFunds);
}
public static decimal GetFinalOutCome(decimal Proceeds, decimal Profit)
// Method to calculate the final total made from selling granola pars
{
return (Profit - Proceeds);
}
}
Is my program calculating correctly or am I missing what would make it calculate correctly?
This seems to be a problem
I mean you need to times how many cases are sold at least at some point
public static decimal GetProfit(int CasesSold, decimal PricePerBar)
// Method to get the Profit
{
return (((PricePerBar * 12) - PriceofCase) * CasesSold);
}
output
The Computer Club buys the cases at $5.00 a case
The studdent government gets 10% of profit
Total made from sell is number of cases sold minus 10%
Enter the number of cases sold: 2
Enter the price per bar: 10
The amount cases of sold was: 2
The price per bar was $10.00:
The gross profit is: $230.00:
The student government fees are: $23.00
The income minus the student government fees is: $207.00

Categories