C# calculating area and perimeter [closed]
so im using Microsoft Visual C# 2010 Express. im trying to make a program that calculates area and perimeter. i have 2 text boxes for length and width. i have 2 readonly text boxes for area and perimeter. so i put in the numbers for area and perimeter and i get the answers in the area and perimeter readonly boxes when i click the "calculate" button. what's the code for this calculation?
Well that would be simple. Using basic geometrical formulas: Area = height x width and Perimiter = (height + width) x 2
Then simply do this:
int pmtr = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)) * 2;
int area = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text);
textBox3.Text = pmtr.ToString();
textBox4.Text = area.ToString();
Also, you might want to look at TryParse to make the code a bit more stable and avoid exceptions on invalid values.
精彩评论