# Variable & Calculations - Please assist in solving and explaining

• ### Question

• 1. Assume that individuals are taxed at 20% of their income. Obtain an income value from the text box, then calculate and display the initial amount, the amount after deductions, and the deducted amount. Use labels to make the results understandable.
Wednesday, August 7, 2019 3:58 PM

• Hi AbsolomSA,

Thank you for posting here.

I make a simple example based on your description.

Here’s the code, and you need to enter a number in ‘initial income’ and click butten ‘calculate’ to get the values you want to know.

```        private void button1_Click(object sender, EventArgs e)
{
try
{
int income = Convert.ToInt32(textBox1.Text);
textBox2.Text = ((int)(income * (0.8))).ToString();
textBox3.Text = (income - (int)(income * (0.8))).ToString();
}
catch(FormatException ex)
{
}
catch(OverflowException ex)
{
MessageBox.Show(\$"Please enter a number less than {Int32.MaxValue}" );
}

}```

Result of a test:

Best Regards,

Xingyu Zhao

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Proposed as answer by Tuesday, August 13, 2019 12:41 PM
• Marked as answer by Wednesday, August 21, 2019 1:38 PM
Thursday, August 8, 2019 2:33 AM

### All replies

• Hello,

Please show us your current code so we can assist, if there is no existing code please make an effort first as we are not here to write this for you.

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

Wednesday, August 7, 2019 4:05 PM
• Hi AbsolomSA,

Thank you for posting here.

I make a simple example based on your description.

Here’s the code, and you need to enter a number in ‘initial income’ and click butten ‘calculate’ to get the values you want to know.

```        private void button1_Click(object sender, EventArgs e)
{
try
{
int income = Convert.ToInt32(textBox1.Text);
textBox2.Text = ((int)(income * (0.8))).ToString();
textBox3.Text = (income - (int)(income * (0.8))).ToString();
}
catch(FormatException ex)
{
}
catch(OverflowException ex)
{
MessageBox.Show(\$"Please enter a number less than {Int32.MaxValue}" );
}

}```

Result of a test:

Best Regards,

Xingyu Zhao

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Proposed as answer by Tuesday, August 13, 2019 12:41 PM
• Marked as answer by Wednesday, August 21, 2019 1:38 PM
Thursday, August 8, 2019 2:33 AM
• This was obviously a homework assignment.  You should not have written the code for him.

Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

Thursday, August 8, 2019 4:37 AM
• Hi Karen

Apologies, I am a novice and am struggling to understand some calculations and how to structure the code.

This is what I had done thus far:

namespace End_of_Chap_4_exercise_4._5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, System.EventArgs e)
{
int Tax = (20 /100);
int Income = 100;
int pay = (Income * Tax);
label1.Text = ("Income");
label2.Text = ("Income after deductions");
label3.Text = ("Deducted amount");
MessageBox.Show("Pay: " + Convert.ToString(pay));

}
}
}

Thursday, August 8, 2019 5:32 AM
• Greetings AbsolomSA.

There's an obvious problem with these lines.

```           int Tax = (20 /100);
int Income = 100;
int pay = (Income * Tax);```

If you divide 20 by 100 using integers, the result will be zero. Because integers can't hold fractions, so they round down to a whole number. You should use doubles or floats for calculations which require fractions (or decimal for money).

```           double Tax = (20.0 /100.0);
double Income = 100.0;
double pay = (Income * Tax);```
(And pay isn't income times tax. But I'll leave that to you to fix.)

• Edited by Thursday, August 8, 2019 6:16 AM Added pay isn't income times tax.
Thursday, August 8, 2019 6:13 AM
• Thank you Xingyu for your response.

At least I now have a guideline to the answer.

Kind regards,

Absolom

Tuesday, August 13, 2019 7:27 AM
• Hi Tim

Actually it was not part of an assignment but an exercise in the text book that I am using to study C# called C# Sharp for Students by Douglas Bell & Mike Parr, exercise 4.5.

Kind regards,

Absolom

Tuesday, August 13, 2019 7:31 AM
• Thank you Ante

I was just a bundle of confusion when I was trying to figure this out but thanks to your guidance, I think I am now getting the gist of things.

Kind regards,

Absolom

Tuesday, August 13, 2019 7:33 AM
• Hi AbsolomSA,

It seems that your problem has been solved. If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.