Skip to content

Commit

Permalink
Added Complex Number Calculator (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashi19234 authored Feb 7, 2024
1 parent 6d1ae3e commit b2ec056
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Calculators/Complex-Number-Calculator/README.md
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)
27 changes: 27 additions & 0 deletions Calculators/Complex-Number-Calculator/index.html
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>
82 changes: 82 additions & 0 deletions Calculators/Complex-Number-Calculator/script.js
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;
}

62 changes: 62 additions & 0 deletions Calculators/Complex-Number-Calculator/style.css
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;
}


14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,20 @@ <h3>Calculates the time required to finish reading a book.</h3>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Complex Number Calculator</h2>
<h3>Perfoms mathematical operations on complex numbers.</h3>
<div class="card-footer">
<a href="./Calculators/Complex-Number-Calculator/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Complex-Number-Calculator" title="Source Code" target="_blank">
<img src="./assets/images/github.png" alt="Source Code"></img>
</a>
</div>
</div>
</div>
</div>

<!-- Calculator Section Ends Here -->
Expand Down

0 comments on commit b2ec056

Please sign in to comment.