

#Java body mass index calculator code code#
Here is the complete source code for Java program that calculates the BMI value based on the inputs given by the user in metric units. So, according to the obtained BMI value, the if-else statements will be executed. Also, if-else statements are used to categorize the BMI values.

The obtained BMI value is displayed on the console.The height in centimeters can be converted to height in meters by dividing the height value by 100 (1 m = 100 cm).The formulas for calculating BMI areor Create a BMI. (Body Mass Index Calculator) We introduced the body mass index (BMI) calculator in Exercise 1.10. The BMI value is calculated by dividing weight in kg with square of the height in meters. Body Mass Index (BMI) Calculator in Java. Create a Java program to calculate the body mass index (BMI) for a person using the persons height in inches and weight in pounds.A static method CalculateBMI() is called by passing height and weight as its arguments.Input the weight in kg and height in cm from the user.The formula for calculating BMI value is:īMI = Weight in Kg/(Height in Meters * Height in Meters)įor example, let us consider height = 1.5m and weight = 90kgīMI = 90 / (1.5 * 1.5) = 40 (Obese). If (b = 18.5 & b 25 & b 30 & b 35 & b Īfter this step, you can access the BMI calculator module from the HTML file.Body Mass Index categories Formula for Calculating BMI in Metric Units Var b = w/(h*h) // According to the formulaĭocument.getElementById("ans").value = String(b.toPrecision(3)) +" kg/m^2" // writing to the output Write a program to calculate Body Mass Index (BMI) given a persons height and. Var h = parseFloat(document.getElementById("height").value)/100 // Convert height to meters View BMI.java from MATH 2336542666 at James Rickards High School. Var w = parseFloat(document.getElementById("weight").value) // To get weight from the input with id as weight We can classify the BMI into the following categories: Now we need to write a function named bmi to calculate the BMI of the user from the entered values. The interface looks like the below image: The function “document.write” is used to write to the HTML document in which the file “bmi.js” is included. Then write the following code to create an input interface for the user.Create a javascript file by the name of “bmi.js”.Now we should collect mass and height-related information as follows: So, our main goal is to collect information from the height, weight information from the user, and calculate body mass index by the following formula:
#Java body mass index calculator code how to#
How to make a BMI calculator using JavaScriptįirstly, what is the body mass index (BMI)?īody mass index ( BMI) is defined as the body mass divided by the square of the body height and is expressed in units of kg/m 2, resulting from mass in kilograms and height in meters. I am using Visual Studio Code for editing, you can use any editor you are comfortable with. The readers are suggested to use an editor while writing the code. Although a basic understanding of HTML and DOM ( Document Object Model ) will give you an edge. There are no specific pre-requisites for this tutorial. This tutorial will not only enhance your knowledge about BMI calculation but also provide a basic understanding of JavaScript.

This module can be included in any of the HTML files with a single line of code. Hello! In this tutorial, we will be building a body mass index (BMI) calculator in JavaScript.
