views:

118

answers:

3

Actually I am working on bmi calculator. Where I would like to calculate bmi for height in inches and weight in lbs and also in need of correct formula for height in cm and weight in kgs.

I have tried but couldn't calculate actual value coming withing the range as below. It exceeds the range.

BMI Categories:

* Underweight = <18.5
* Normal weight = 18.5-24.9
* Overweight = 25-29.9
* Obesity = BMI of 30 or greater
A: 

BMI in imperial units:

BMI = weight(lb) x 703 / height(in)^2

Ref.: BMI on Wikipedia.

jensgram
A: 

I just guessed that your problem is with float and integer. You have to make sure you use CGFloat weight for float so that it can contains floating point number

Then you just do a lot of if else

CGFloat BMI = weight(lb) x 703 / height * height

if (BMI <= 18.5) 
  NSLog(@"Under weight");
vodkhang
`^` in C is a bitwise XOR, not an exponential operator.
Nick Forge
ah yeah, it is a problem, I will try to solve it
vodkhang
+1  A: 

Here you go (this assumes your original values are ints - you can easily use floats instead without any problems):

// Metric
int heightInCms;
int weightInKgs;

// Imperial
int heightInInches;
int weightInPounds;

float height;   // metres
float weight;   // kilograms

// Unit Conversions
height = (float) heightInCms * 0.01;
weight = (float) weightInKgs;

height = (float) heightInInches * 0.0254;
weight = (float) weightInPounds * 0.4536;

float BMI = weight / (height * height);

if (BMI < 18.5) {
    // Underweight
}
else if (BMI < 25.0) {
    // Normal
}
else if (BMI < 30.0) {
    // Overweight
}
else {
    // Obese
}
Nick Forge