# How to Calculate Your BMI Using C Code

Written by jolene lewis
• Share
• Tweet
• Share
• Pin
• Email

If you are passionate about health and technology, you may be interested in calculating your body mass index (BMI) using C code. BMI is calculated using your height-to-weight ratio and sometimes takes into consideration your age and sex. According to the Weight-control Information Network, a high BMI may put you at greater risk for certain types of cancer, coronary heart disease, gallbladder disease, osteoarthritis, and pregnancy complications. A BMI of 18.5 to 24.9 is considered healthiest, while a BMI of 25 to 29.9 and 30 or higher are considered overweight and obese, respectively.

Skill level:
Easy

### Things you need

• Adult weight scale, in lbs.
• Measuring tape

## Instructions

1. 1

Set up your C++ BMI code on Windows or Linux/Unix platforms.

2. 2

Go to www.arachnoid.com to set up your C++ BMI code on Windows.

3. 3

Go to www.arachnoid.com C++ tutorial page for C++ BMI code set-up on Unix.

4. 4

Execute code lines 1 to 59 as follows onto the appropriate platform:

01 using System;

02 using System.Collections.Generic;

03 using System.ComponentModel;

04 using System.Data;

05 using System.Drawing;

06 using System.Linq;

07 using System.Text;

08 using System.Windows.Forms;

09

10 namespace BMIApp

11 {

12 public partial class Form1 : Form

13 {

14 public Form1()

15 {

16 InitializeComponent();

17 }

18

19 private void btnCalculate_Click(object sender, EventArgs e)

20 {

21 double weght = string.IsNullOrEmpty(txtWeight.Text) ? 1 : double.Parse(txtWeight.Text);

22 double height = string.IsNullOrEmpty(txtHeight.Text) ? 1 : double.Parse(txtHeight.Text);

23

24 if (weght == 0)

25 {

26 MessageBox.Show("Results will be inaccurate. Weight is not a valid number.");

27 }

28 if (height == 0)

29 {

30 MessageBox.Show("Results will be inaccurate. Height is not a valid number.");

31 }

32

33 double vmult = cboWeightUnits.SelectedItem.ToString() == "pounds" ? 2.204 : 1;

34 double hmult = cboHeightUnits.SelectedItem.ToString() == "inches" ? 0.0254 : 1;

35

36 double BMI = Math.Round(((weght / vmult) / ((height * hmult) * (height * hmult))) * 10) / 10;

37

38

39 string BMI_description = string.Empty;

40 if (BMI < 16.5)

41 BMI_description = "severely underweight";

42 else if (BMI >= 16.5 && BMI < 18.5)

43 BMI_description = "underweight";

44 else if (BMI >= 18.5 && BMI < 25)

45 BMI_description = "normal";

46 else if (BMI >= 25 && BMI <= 30)

47 BMI_description = "overweight";

48 else if (BMI > 30 && BMI <= 35)

49 BMI_description = "obese";

50 else if (BMI > 35 && BMI <= 40)

51 BMI_description = "clinically obese";

52 else

53 BMI_description = "morbidly obese";

54

55

56 txtResult.Text = string.Format("Your Body Mass Index (BMI) is: {0}. This would be considered {1}.", BMI, BMI_description);

57 }

58 }

59 }

1. 1

Take your weight in lbs. and enter it into the weight category box of the C++ BMI calculator.

2. 2

Measure your height in inches and input it into the height box of the C++ BMI calculator.

3. 3

Determine if your weight class is healthy, overweight or obese, based on the calculator output.

#### Tips and warnings

• Please see the Resources section for additional BMI calculators and further information related to body mass index.

### Don't Miss

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent