fork download
  1. //Devin Scheu CS1A Chapter 4, P. 220, #5
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE BODY MASS INDEX (BMI)
  6. * ____________________________________________________________
  7. * This program calculates a person's body mass index (BMI)
  8. * based on weight (in pounds) and height (in inches). It then
  9. * determines if the person is underweight, optimal weight, or
  10. * overweight.
  11. *
  12. * Computation is based on the formula:
  13. * BMI = weight * 703 / (height * height)
  14. * Optimal BMI range: 18.5 to 25
  15. * ____________________________________________________________
  16. * INPUT
  17. * weight : Weight in pounds (as a double)
  18. * height : Height in inches (as a double)
  19. *
  20. * OUTPUT
  21. * message : Indicates whether the person is underweight, optimal, or overweight
  22. *
  23. **************************************************************/
  24.  
  25. #include <iostream>
  26. #include <iomanip>
  27.  
  28. using namespace std;
  29.  
  30. int main () {
  31.  
  32. //Variable Declarations
  33. double weight; //INPUT - Weight in pounds (as a double)
  34. double height; //INPUT - Height in inches (as a double)
  35. double bmi; //PROCESSING - Body mass index calculated from weight and height
  36. string message; //OUTPUT - Indicates whether the person is underweight, optimal, or overweight
  37.  
  38. //Prompt for Input
  39. cout << "Enter your weight in pounds: ";
  40. cin >> weight;
  41. cout << "\nEnter your height in inches: ";
  42. cin >> height;
  43.  
  44. //Calculate BMI
  45. bmi = weight * 703 / (height * height);
  46.  
  47. //Determine Weight Status
  48. if (bmi < 18.5) {
  49. message = "You are underweight.";
  50. } else if (bmi >= 18.5 && bmi <= 25) {
  51. message = "You have optimal weight.";
  52. } else {
  53. message = "You are overweight.";
  54. }
  55.  
  56. //Output Result
  57. cout << fixed << setprecision(2);
  58. cout << "\n";
  59. cout << left << setw(25) << "Weight:" << right << setw(15) << weight << " lbs" << endl;
  60. cout << left << setw(25) << "Height:" << right << setw(15) << height << " in" << endl;
  61. cout << left << setw(25) << "BMI:" << right << setw(15) << bmi << endl;
  62. cout << left << setw(25) << "Status:" << right << setw(15) << message << endl;
  63.  
  64. } //end of main()
Success #stdin #stdout 0.01s 5316KB
stdin
194
69
stdout
Enter your weight in pounds: 
Enter your height in inches: 
Weight:                           194.00 lbs
Height:                            69.00 in
BMI:                               28.65
Status:                  You are overweight.