Hello Friends Today, through this tutorial, I will tell you How to Write Program Ohm’s Law calculator using PHP with HTML. Below is a simple HTML file with JavaScript code to create an Ohm’s Law calculator. This calculator will allow users to calculate voltage, current, or resistance based on the values they input:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ohm's Law Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
width: 80%;
margin: 0 auto;
}
input[type="number"] {
width: 100px;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h2>Ohm's Law Calculator</h2>
<label for="voltage">Voltage (V):</label>
<input type="number" id="voltage" step="any"><br><br>
<label for="current">Current (I):</label>
<input type="number" id="current" step="any"><br><br>
<label for="resistance">Resistance (R):</label>
<input type="number" id="resistance" step="any"><br><br>
<button onclick="calculate()">Calculate</button><br><br>
<div id="result"></div>
</div>
<script>
function calculate() {
const voltage = document.getElementById('voltage').value;
const current = document.getElementById('current').value;
const resistance = document.getElementById('resistance').value;
let result = "";
if (voltage === "" && current !== "" && resistance !== "") {
result = "Voltage (V) = " + (current * resistance) + " volts";
} else if (voltage !== "" && current === "" && resistance !== "") {
result = "Current (I) = " + (voltage / resistance) + " amperes";
} else if (voltage !== "" && current !== "" && resistance === "") {
result = "Resistance (R) = " + (voltage / current) + " ohms";
} else {
result = "Please provide values for two fields.";
}
document.getElementById('result').innerText = result;
}
</script>
</body>
</html>
This code creates an HTML page with input fields for voltage, current, and resistance, along with a button to calculate the missing value. When the user clicks the “Calculate” button, the `calculate()` function is called, which determines which value is missing based on the inputs provided and displays the result.