Bmi Calculator
Bmi Calculator
BMI CALCULATOR
DATE:
AIM:
To Using react native, build a cross platform application for a BMI calculator.
ALGORITHM:
Step 1: Start
Step 2: Initialize state variables for weight, height, BMI, and message.
Step 3: Create a function to calculate BMI when the button is clicked.
Step 4: Check if both weight and height are entered. If not, display a message to enter both
values.
Step 5: Convert height to meters and calculate BMI using the formula: BMI = weight / (height *
height).
Step 6: Set the BMI value and display a message based on the BMI category.
Step 7: Stop the program.
PROGRAM:
APP.JS
import React, { useState } from "react";
import "./App.css";
function App() {
const [weight, setWeight] = useState("");
const [height, setHeight] = useState("");
const [bmi, setBmi] = useState(null);
const [message, setMessage] = useState("");
const calculateBMI = () => {
if (!weight || !height) {
setMessage("Please enter both weight and height.");
return;
}
const heightInMeters = height / 100;
const bmiResult = weight / (heightInMeters *
heightInMeters);setBmi(bmiResult.toFixed(2));
return (
<div className="App">
<h1>BMI Calculator</h1>
<div className="input-container">
<label>Weight (kg):</label>
<input
type="number"
value={weight}
onChange={(e) => setWeight(e.target.value)}
placeholder="Enter your weight"
/>
</div>
<div className="input-container">
<label>Height (cm):</label>
<input
type="number"
value={height}
onChange={(e) => setHeight(e.target.value)}
placeholder="Enter your height"
/>
</div>
<button onClick={calculateBMI}>Calculate BMI</button>
{bmi && (
<div className="result">
<h2>Your BMI: {bmi}</h2>
<p>{message}</p>
</div>
)}
</div>
);
}