Gunnar
Gunnar

Reputation: 113

Trouble with my BMI program C#?

I am having trouble with my bmi calculator. Here are the details:

Write a program that takes a person's height and weight in pounds and returns the body mass index(BMI). BMI is defined as the weight, expressed in kilograms, *divided by the square of the height expressed in meters.*
One inch is 0.0254 meters and one pound is 0.454 kilograms.

This is a windows form app btw.

Well when I am trying to square the height using ^, it gives me an error: Operator '^'...

Here is my code:

private void button1_Click(object sender, EventArgs e)
{
    //Declare variables.
    decimal heightDecimal ;
    decimal weightDecimal;
    decimal bmiDecimal;
    decimal resultDecimal;

    //Get user input.
    heightDecimal = Decimal.Parse(txtHeight.Text);
    weightDecimal = Decimal.Parse(txtWeight.Text);

    //Calculations.
    weightDecimal = (Decimal)0.454;
    heightDecimal = (Decimal)0.0254;
    bmiDecimal = weightDecimal / heightDecimal ^ 2;




    //Display.
    lblBMI.Text = bmiDecimal.ToString();
}

I am trying to figure out the calculations. I am confused. Can anyone please help me? Thanks.

Tested what everyone said. I got some weird numbers. I started it and I put 5 for my height and 100 for my weight(random) and I got 700? Are my calculations wrong?

Upvotes: 1

Views: 4523

Answers (5)

VasanthRavichandran
VasanthRavichandran

Reputation: 366

Weight = Convert.ToDecimal(txtWeight.Text); Height = Convert.ToDecimal(txtHeight.Text);

            BodyMassIndex = (Weight * 703) / (Height * Height);

            txtMassIndex.Text = Convert.ToString(Math.Round(BodyMassIndex, 4) + " lbs/ Inch Square");

Upvotes: 0

John DeLeon
John DeLeon

Reputation: 305

Here's what it would look like in a Console App:

        decimal feetDecimal;
        decimal inchesDecimal;
        decimal weightDecimal;
        decimal bmiDecimal;
        decimal resultDecimal;


        //Get user input.
        Console.WriteLine("Enter feet:");
        feetDecimal = Decimal.Parse(Console.ReadLine());
        Console.WriteLine("Enter inches:");
        inchesDecimal = Decimal.Parse(Console.ReadLine());
        Console.WriteLine("Enter weight in pounds:");
        weightDecimal = Decimal.Parse(Console.ReadLine());

        //Calculations. 
        inchesDecimal += feetDecimal * 12;
        decimal height = inchesDecimal * (decimal)0.0254;
        decimal weight = weightDecimal * (decimal)0.453592;
        bmiDecimal = weight / (height * height);
        //Display.
        Console.WriteLine(bmiDecimal.ToString());
        Console.ReadLine();

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34844

The .NET Framework also provides a Math class that has a Pow method, which allows for squaring of numbers like this:

Math.Pow(2, 2)

That is 2 squared, which equals 4.

Your code would be:

bmiDecimal = weightDecimal / Math.Pow(heightDecimal, 2);

Note: For more information read documentation for Math.Pow.

Upvotes: 0

TGH
TGH

Reputation: 39278

bmiDecimal = weightDecimal / (heightDecimal * heightDecimal);

Try the above. ^ is xor

Alternatively

bmiDecimal = weightDecimal / Math.Pow(heightDecimal, 2)

Some test values could be 90 kg and 1.80 m

90 / (1.80 * 1.80)

90 kg is roughly 200 lbs and 1.80 m is 5.11 if you're not used to the metric system

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160992

bmiDecimal = weightDecimal / heightDecimal ^ 2;

You probably meant

bmiDecimal = weightDecimal / (heightDecimal  * heightDecimal);

^ is the XOR operator in C#.

Edit: If you don't use metric unit, you have to multiply the results by 703.06957964, see Wikipedia.

Upvotes: 2

Related Questions