-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Coulombs Law Calculator (#1964)
- Loading branch information
1 parent
b782852
commit c96d011
Showing
6 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# <p align="center">Coulombs Law Calculator</p> | ||
|
||
## Description :- | ||
|
||
This project is a web-based calculator that implements **Coulomb's Law**: | ||
|
||
\[ F = k \frac{q_1 \cdot q_2}{r^2} \] | ||
|
||
It allows users to calculate the force between two charges (\( F \)) based on: | ||
|
||
- Charge 1 (\( q_1 \)) | ||
- Charge 2 (\( q_2 \)) | ||
- Distance between the charges (\( r \)) | ||
|
||
The value of \( k \) (Coulomb's constant) is also displayed: | ||
\[ k = 8.987 \times 10^9 \, \text{N·m}^2/\text{C}^2 \] | ||
|
||
## Tech Stacks :- | ||
|
||
- HTML | ||
- CSS | ||
- JavaScript | ||
|
||
## Features :- | ||
|
||
- **Dynamic Formula Display**: The formula and constant are displayed in LaTeX for clarity. | ||
- **Real-Time Results**: Automatically calculates the force when valid inputs are provided. | ||
- **Modern UI**: A responsive design with a stylish dark blue card background. | ||
- **Error Handling**: Ensures inputs are valid and displays appropriate error messages. | ||
|
||
## How to Use :- | ||
|
||
1. Enter the values for: | ||
- Charge 1 (\( q_1 \)) in Coulombs (C) | ||
- Charge 2 (\( q_2 \)) in Coulombs (C) | ||
- Distance (\( r \)) between charges in meters (m). | ||
2. Click the **Calculate** button to compute the force (\( F \)). | ||
3. Click the **Clear** button to reset the inputs and results. | ||
|
||
### Example Calculation :- | ||
|
||
#### Case: | ||
- \( q_1 = 2 \, \text{C} \) | ||
- \( q_2 = 3 \, \text{C} \) | ||
- \( r = 0.5 \, \text{m} \) | ||
|
||
#### Output: | ||
- Force (\( F \)): | ||
\[ | ||
F = \frac{k \cdot q_1 \cdot q_2}{r^2} = \frac{8.987 \times 10^9 \cdot 2 \cdot 3}{0.5^2} = 2.157 \times 10^{11} \, \text{N} | ||
\] | ||
|
||
## Screenshots :- | ||
|
||
![image](https://github.com/user-attachments/assets/5ebb2959-94a2-4be8-bbe3-72fac99e44d5) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="stylesheet" href="style.css"> | ||
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> | ||
<title>Coulombs Law Calculator</title> | ||
</head> | ||
|
||
<body> | ||
<div class="calculator"> | ||
<h1>Coulomb's Law Calculator</h1> | ||
<p class="formula"> | ||
Formula: | ||
<span id="latex-formula">\( F = k \frac{q_1 \cdot q_2}{r^2} \)</span> | ||
</p> | ||
<p class="constant"> | ||
Where \( k = 8.987 \times 10^9 \, \text{N·m}^2/\text{C}^2 \) | ||
</p> | ||
|
||
<label for="q1">Charge 1 (\( q_1 \)) (C):</label> | ||
<input type="number" id="q1" placeholder="Enter Charge 1"> | ||
|
||
<label for="q2">Charge 2 (\( q_2 \)) (C):</label> | ||
<input type="number" id="q2" placeholder="Enter Charge 2"> | ||
|
||
<label for="r">Distance (\( r \)) (m):</label> | ||
<input type="number" id="r" placeholder="Enter Distance"> | ||
|
||
<div class="buttons"> | ||
<button onclick="calculateForce()">Calculate</button> | ||
<button onclick="clearInputs()">Clear</button> | ||
</div> | ||
<div class="result" id="result"></div> | ||
</div> | ||
|
||
<script src="script.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function calculateForce() { | ||
const q1 = parseFloat(document.getElementById('q1').value); | ||
const q2 = parseFloat(document.getElementById('q2').value); | ||
const r = parseFloat(document.getElementById('r').value); | ||
|
||
if (isNaN(q1) || isNaN(q2) || isNaN(r) || r <= 0) { | ||
document.getElementById('result').innerText = "Please enter valid numbers for all fields, and ensure distance is greater than zero."; | ||
return; | ||
} | ||
|
||
const k = 8.9875517923e9; // Coulomb's constant in N·m²/C² | ||
const force = (k * q1 * q2) / (r ** 2); | ||
|
||
document.getElementById('result').innerText = `Force (F): ${force.toFixed(2)} N`; | ||
} | ||
|
||
function clearInputs() { | ||
document.getElementById('q1').value = ''; | ||
document.getElementById('q2').value = ''; | ||
document.getElementById('r').value = ''; | ||
document.getElementById('result').innerText = ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
body { | ||
font-family: 'Roboto', sans-serif; | ||
background-image: url('assets/background.webp'); | ||
background-size: cover; | ||
margin: 0; | ||
padding: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
color: #fff; | ||
height: 100%; | ||
min-height: 100vh; | ||
} | ||
|
||
.calculator { | ||
background: #0d47a1; /* Dark blue background */ | ||
padding: 30px; | ||
border-radius: 10px; | ||
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); | ||
width: 400px; | ||
text-align: center; | ||
} | ||
|
||
h1 { | ||
font-size: 28px; | ||
margin-bottom: 20px; | ||
color: #ffeb3b; | ||
} | ||
|
||
.formula { | ||
font-size: 16px; | ||
margin-bottom: 10px; | ||
color: #ffcc80; | ||
} | ||
|
||
.constant { | ||
font-size: 14px; | ||
margin-bottom: 20px; | ||
color: #ffcc80; | ||
} | ||
|
||
label { | ||
font-size: 14px; | ||
margin: 10px 0 5px; | ||
display: block; | ||
text-align: left; | ||
color: #bbdefb; | ||
} | ||
|
||
input { | ||
width: calc(100% - 20px); | ||
padding: 10px; | ||
margin-bottom: 15px; | ||
font-size: 16px; | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
} | ||
|
||
.buttons { | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
|
||
button { | ||
background-color: #29b6f6; | ||
color: white; | ||
padding: 12px 20px; | ||
border: none; | ||
border-radius: 5px; | ||
font-size: 16px; | ||
cursor: pointer; | ||
margin: 5px; | ||
flex: 1; | ||
} | ||
|
||
button:hover { | ||
background-color: #0288d1; | ||
} | ||
|
||
button:last-child { | ||
background-color: #f44336; | ||
} | ||
|
||
button:last-child:hover { | ||
background-color: #d32f2f; | ||
} | ||
|
||
.result { | ||
margin-top: 20px; | ||
font-size: 18px; | ||
font-weight: bold; | ||
color: #fff; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters