Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Complex Number Calculator #562

Merged
merged 7 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Calculators/Complex-num-calc/Screenshot (86).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions Calculators/Complex-num-calc/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>
14 changes: 14 additions & 0 deletions Calculators/Complex-num-calc/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

### Sample Images:
![alt text](<Screenshot (86).png>)
82 changes: 82 additions & 0 deletions Calculators/Complex-num-calc/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-num-calc/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;
}


17 changes: 16 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,22 @@ <h3>Calculates the typing speed in two different units.</h3>
</div>
</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-num-calc/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Complex-num-calc" 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
Loading