Today I started creating a project in Visual Studio 2017 in C# with Windows Forms. The program should calculate the volume of a pyramid with a rectangular base and display the result.
The problem is that if I click the button which should reveal the result, the result is 0.
Heres the important code:
// stores height h, side1 a, side2 b
double a, b, h;
//Reads out b from a textbox
private void txbB_TextChanged(object sender, EventArgs e)
{
string parseB = txbB.Text;
b = double.Parse(parseB);
}
//Reads out a from a textbox
private void txbA_TextChanged(object sender, EventArgs e)
{
string parseA = txbA.Text;
a = double.Parse(parseA);
}
//Reads out h from a textbox
private void txbH_TextChanged(object sender, EventArgs e)
{
string parseH = txbH.Text;
h = double.Parse(parseH);
}
//button which calculates the volume of the pyramid
//when clicked and prints it to the label "LblErgebnis"
private void Cmdrechnen_Click(object sender, EventArgs e)
{
double Total = 1/3 * h * a * b;
string Result = Total.ToString();
LblErgebnis.Text = Result;
}
Can someone tell me why the result is always 0?
In the line double Total = 1/3 * h * a * b;
, the 1/3
is integer division, resulting in 0. Change that to 1.0 / 3.0
.