-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Complex Number Calculator (#562)
- Loading branch information
1 parent
6d1ae3e
commit b2ec056
Showing
5 changed files
with
199 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,14 @@ | ||
# <p align="center">Complex Number Calculator</p> | ||
|
||
## Description :- | ||
|
||
It is a calculator which handles complex numbers and their operations, such as addition, subtraction, multiplication, and division, and provides both rectangular and polar forms of the result. | ||
|
||
## Tech Stacks:- | ||
|
||
- HTML | ||
- CSS | ||
- JavaScript | ||
|
||
### Screenshots:- | ||
![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/5276968a-c78f-4028-9fc6-c9838a72cf32) |
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,27 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Complex Number Calculator</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h2 style="color: #4CAF50; text-align: center;">Complex Number Calculator</h2> | ||
<input type="number" id="real1" placeholder="Enter Real Part of First Number"> | ||
<input type="number" id="imaginary1" placeholder="Enter Imaginary Part of First Number"> | ||
<input type="number" id="real2" placeholder="Enter Real Part of Second Number"> | ||
<input type="number" id="imaginary2" placeholder="Enter Imaginary Part of Second Number"> | ||
<button onclick="add()">Add</button> | ||
<button onclick="subtract()">Subtract</button> | ||
<button onclick="multiply()">Multiply</button> | ||
<button onclick="divide()">Divide</button> | ||
<button onclick="reset()">Reset</button> | ||
<div id="result" style="text-align: center;"></div> | ||
</div> | ||
|
||
|
||
</body> | ||
<script src="script.js"></script> | ||
</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,82 @@ | ||
function add() { | ||
if (validateInput()) { | ||
var real1 = parseFloat(document.getElementById('real1').value); | ||
var imaginary1 = parseFloat(document.getElementById('imaginary1').value); | ||
var real2 = parseFloat(document.getElementById('real2').value); | ||
var imaginary2 = parseFloat(document.getElementById('imaginary2').value); | ||
var resultReal = real1 + real2; | ||
var resultImaginary = imaginary1 + imaginary2; | ||
displayResult(resultReal, resultImaginary); | ||
} | ||
} | ||
|
||
function subtract() { | ||
if (validateInput()) { | ||
var real1 = parseFloat(document.getElementById('real1').value); | ||
var imaginary1 = parseFloat(document.getElementById('imaginary1').value); | ||
var real2 = parseFloat(document.getElementById('real2').value); | ||
var imaginary2 = parseFloat(document.getElementById('imaginary2').value); | ||
var resultReal = real1 - real2; | ||
var resultImaginary = imaginary1 - imaginary2; | ||
displayResult(resultReal, resultImaginary); | ||
} | ||
} | ||
|
||
function multiply() { | ||
if (validateInput()) { | ||
var real1 = parseFloat(document.getElementById('real1').value); | ||
var imaginary1 = parseFloat(document.getElementById('imaginary1').value); | ||
var real2 = parseFloat(document.getElementById('real2').value); | ||
var imaginary2 = parseFloat(document.getElementById('imaginary2').value); | ||
var resultReal = real1 * real2 - imaginary1 * imaginary2; | ||
var resultImaginary = real1 * imaginary2 + real2 * imaginary1; | ||
displayResult(resultReal, resultImaginary); | ||
} | ||
} | ||
|
||
function divide() { | ||
if (validateInput()) { | ||
var real1 = parseFloat(document.getElementById('real1').value); | ||
var imaginary1 = parseFloat(document.getElementById('imaginary1').value); | ||
var real2 = parseFloat(document.getElementById('real2').value); | ||
var imaginary2 = parseFloat(document.getElementById('imaginary2').value); | ||
if (real2 === 0 && imaginary2 === 0) { | ||
document.getElementById('result').innerHTML = 'Cannot divide by zero'; | ||
return; | ||
} | ||
var denominator = real2 * real2 + imaginary2 * imaginary2; | ||
var resultReal = (real1 * real2 + imaginary1 * imaginary2) / denominator; | ||
var resultImaginary = (imaginary1 * real2 - real1 * imaginary2) / denominator; | ||
displayResult(resultReal, resultImaginary); | ||
} | ||
} | ||
|
||
function reset() { | ||
document.getElementById('real1').value = ''; | ||
document.getElementById('imaginary1').value = ''; | ||
document.getElementById('real2').value = ''; | ||
document.getElementById('imaginary2').value = ''; | ||
document.getElementById('result').innerHTML = ''; | ||
} | ||
|
||
function validateInput() { | ||
var real1 = document.getElementById('real1').value; | ||
var imaginary1 = document.getElementById('imaginary1').value; | ||
var real2 = document.getElementById('real2').value; | ||
var imaginary2 = document.getElementById('imaginary2').value; | ||
if (real1 === '' || imaginary1 === '' || real2 === '' || imaginary2 === '') { | ||
document.getElementById('result').innerHTML = 'Enter a valid value'; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function displayResult(real, imaginary) { | ||
var result = document.getElementById('result'); | ||
var rectForm = 'Rectangular Form: ' + real + ' + ' + imaginary + 'i<br>'; | ||
var magnitude = Math.sqrt(real * real + imaginary * imaginary); | ||
var angle = Math.atan2(imaginary, real); | ||
var polarForm = 'Polar Form: ' + magnitude.toFixed(2) + ' * (cos(' + angle.toFixed(2) + ') + i * sin(' + angle.toFixed(2) + '))'; | ||
result.innerHTML = rectForm + polarForm; | ||
} | ||
|
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,62 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
background: linear-gradient(135deg, #ff6e7f, #bfe9ff); | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
margin: 0; | ||
} | ||
|
||
.container { | ||
max-width: 400px; | ||
padding: 20px; | ||
border-radius: 20px; | ||
background-color: rgba(255, 255, 255, 0.3); | ||
backdrop-filter: blur(10px); | ||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1); | ||
} | ||
|
||
input[type="number"] { | ||
width: calc(100% - 22px); | ||
padding: 10px; | ||
margin: 5px 0; | ||
box-sizing: border-box; | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
} | ||
|
||
button { | ||
width: 100%; | ||
padding: 10px; | ||
background-color: #4CAF50; | ||
color: white; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
margin-top: 10px; | ||
transition: background-color 0.3s ease; | ||
} | ||
|
||
button:hover { | ||
background-color: #45a049; | ||
} | ||
|
||
button:active { | ||
background-color: #3e8e41; | ||
} | ||
|
||
h1 { | ||
color: #fff; | ||
text-align: center; | ||
margin-bottom: 20px; | ||
} | ||
|
||
#result { | ||
margin-top: 20px; | ||
padding: 15px; | ||
background-color: rgba(255, 255, 255, 0.7); | ||
border-radius: 5px; | ||
} | ||
|
||
|
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