Question

In: Computer Science

I'm trying to get an understanding of this code as it was done in class, can...

I'm trying to get an understanding of this code as it was done in class, can someone please explain step by step what is happening?

namespace BMI
{
class Program
{
static void Main(string[] args)
{
double height, weight;
Console.Write("Enter person's height in inches: ");
bool value1 = double.TryParse(Console.ReadLine(), out height);

Console.Write("Enter weight in pounds: ");
bool value2 = double.TryParse(Console.ReadLine(), out weight);
if (!value1 || !value2)
{
Console.WriteLine("Not valid input.");
}
else
{
if (height < 5 || height > 120)
{
Console.WriteLine("Not valid height.");
}
else if (weight < 0.5 || weight > 999)
{
Console.WriteLine("Not valid weight.");
}
else
{
double bmi = ((weight / (height * height)) * 703.0);
Console.WriteLine($"The BMI for someone who weighs {weight} lbs and is {height} in tall is " + Math.Round(bmi) + ".");

string catagory = "";
if (bmi < 16)
catagory = "severly underweight";
else if (bmi >= 16 && bmi < 18.5)
catagory = "underweight";
else if (bmi >= 18.5 && bmi < 25)
catagory = "healthy";
else if (bmi >= 25 && bmi < 30)
catagory = "overweight";
else
catagory = "obese";
Console.WriteLine("BMI Catagory: You are " + catagory + ".");
}
}
Console.WriteLine("\nPress any key to close the application:"); //newline operator
Console.ReadKey(); //terminates runtime
}
}
}

Solutions

Expert Solution

C# PROGRAM

using System;
namespace BMI // Calculating BMI(Body Mass Index)
{
class Program
{
static void Main(string[] args)
{
double height, weight; // Declare two double variables height, weight
Console.Write("Enter person's height in inches: ");

bool value1 = double.TryParse(Console.ReadLine(), out height); // input height i.e., represent value1

Console.Write("Enter weight in pounds: ");
bool value2 = double.TryParse(Console.ReadLine(), out weight); // input weight i.e., represent value2
if (!value1 || !value2) // check not valid input values height and weight
{
Console.WriteLine("Not valid input."); // display invalid inputs
}
else
{
if (height < 5 || height > 120) // check in height in inches height<5 or height>120 then,
{
Console.WriteLine("Not valid height."); // display this Message
}
else if (weight < 0.5 || weight > 999) // else, check wieght in pounds weight<0.5 or wieght >999 then,
{
Console.WriteLine("Not valid weight."); // display this message
}
else
{
// else calculate Body Mass Index (BMI) Imperial (means which uses units of pounds and inches) Formula bmi= 703.0*(weight/(height*height)) units kg/m^2
double bmi = ((weight / (height * height)) * 703.0);
Console.WriteLine($"The BMI for someone who weighs {weight} lbs and is {height} in tall is " + Math.Round(bmi) + "."); // display BMI value with rounded using round(bmi)

string catagory = ""; // Declare category means body fact content based on weight and height i.e., calculate bmi

if (bmi < 16) // check bmi<16 then, display 'severly underweight'
catagory = "severly underweight";
else if (bmi >= 16 && bmi < 18.5) // else, check bmi in between 16-18.5 then, display 'underweight'
catagory = "underweight";
else if (bmi >= 18.5 && bmi < 25) // else, check bmi in between 18.5-25 then, display 'healthy'
catagory = "healthy";
else if (bmi >= 25 && bmi < 30) // else, check bmi in between 25-30 then, display 'overweight'
catagory = "overweight";
else
catagory = "obese"; // else, bmi more than 30 then display 'obese'
Console.WriteLine("BMI Catagory: You are " + catagory + "."); // display category
}
}
Console.WriteLine("\nPress any key to close the application:"); //newline operator
Console.ReadKey(); //terminates runtime
}
}
}

OUTPUT-1

Enter person's height in inches: 90
Enter weight in pounds: 60
The BMI for someone who weighs 60 lbs and is 90 in tall is 5.
BMI Catagory: You are severly underweight.

Press any key to close the application:

OUTPUT-2

Enter person's height in inches: 125
Enter weight in pounds: 60
Not valid height.

Press any key to close the application:


Related Solutions

I'm trying to get my code to loop back to let the user input multiple patients....
I'm trying to get my code to loop back to let the user input multiple patients. import java.util.Scanner; public class XYZ {    public static void main(String[] args) {           String response;        do{            //import scanner for input            Scanner input = new Scanner(System.in);               //prompt user input for last name            System.out.print("Enter last name ");            //output for last name            String...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
Javascript array of objects: I'm trying to get the input into an array of objects, and...
Javascript array of objects: I'm trying to get the input into an array of objects, and then display the information in a table using a for loop, but I don't think my path is right <!DOCTYPE html> <head> <title>Form</title> <meta charset="utf-8" /> <style media="screen"> h1 { text-align: center; } div { background-color: #pink; border: 2px solid green; padding: 15px; margin: 65px; } </style> <script> var list = []; total = 0; text = ""; function Product(item, quantity, costs){ this.item =...
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I'm trying to get the union of two arrays and I tried making a loop that...
I'm trying to get the union of two arrays and I tried making a loop that does so. I tried also making the loop give me the size of the array as well from the union of the two arrays. Please check my loop to see what is wrong with it because it is not doing what I want it to do. I'm also not sure how to get the correct size of the array after the two arrays have...
I'm trying to get my occupancy rate to output as 2 decimal places with a %...
I'm trying to get my occupancy rate to output as 2 decimal places with a % sign. I know this is a string but I need it to print as 2 decimal places like 59.23% or 25.36% I have system.out.printf("Occupancy rate: %2s", occupancy + "%"); but I'm still not getting 2 decimal places or the % sign. How can I fix this? I'm in beginning java so I can't use anything advanced. public class ResortAccomidations { public static void main(String[]...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void loop() { // Print the value inside of myBPM. Serial.begin(9600); int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened". Serial.print("BPM:...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT