Skip to content

Commit

Permalink
Added Net Present Value Calculator (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manav173 authored May 24, 2024
1 parent 5515750 commit a4eb8c8
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Calculators/NPV-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# <p align="center">NPV Calculator</p>

## Description :-

Given an initial investment, the discount rate (per year), and the yearly cash flows, this calculator calculates the NPV (Net Present Value) of the investment. NPV is a financial metric used to evaluate the profitability of an investment or project.

## Tech Stacks :-

- HTML
- CSS
- JavaScript

## Formula Used :-

The formula for calculating NPV is:

\[ NPV = \sum \left( \frac{CF_t}{(1 + r)^t} \right) - C_0 \]

Where:
- \( CF_t \) is the cash flow at time \( t \)
- \( r \) is the discount rate
- \( C_0 \) is the initial investment
- \( t \) is the time period (year)

## Screenshots :-

![image](https://github.com/Manav173/CalcDiverse/assets/73993775/640d6aa4-b68b-4057-865c-bee17e8c7e1b)
36 changes: 36 additions & 0 deletions Calculators/NPV-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!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">
<title>NPV Calculator</title>
</head>

<body>
<div class="calculator-container">
<h1>Net Present Value (NPV) Calculator</h1>
<div class="input-container">
<label for="initialInvestment">Initial Investment (INR)</label>
<input id="initialInvestment" type="number" />
</div>
<div class="input-container">
<label for="discountRate">Discount Rate (%)</label>
<input id="discountRate" type="number" />
</div>
<div class="input-container">
<label>Cash Flows</label>
<div id="cashFlows"></div>
<button type="button" onclick="addCashFlow()">+ Add Year</button>
</div>
<button type="button" onclick="calculateNPV()">Calculate</button>
<div class="result-container">
<div id="result">NPV: <span id="npvResult" class="red">Rs. 0.00</span></div>
</div>
</div>

<script src="script.js"></script>
</body>

</html>
42 changes: 42 additions & 0 deletions Calculators/NPV-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
let cashFlowCount = 0;

function addCashFlow() {
cashFlowCount++;
const cashFlowsDiv = document.getElementById('cashFlows');
const newCashFlow = document.createElement('div');
newCashFlow.className = 'cash-flow-item';
newCashFlow.innerHTML = `
<label for="cashFlow${cashFlowCount}">Year ${cashFlowCount}: <br> ₹</label>
<input id="cashFlow${cashFlowCount}" type="number"/>
<button type="button" onclick="removeCashFlow(this); cashFlowCount--;">x</button>
`;
cashFlowsDiv.appendChild(newCashFlow);
}

function removeCashFlow(button) {
const cashFlowItem = button.parentElement;
cashFlowItem.remove();
}

function calculateNPV() {
const initialInvestment = parseFloat(document.getElementById('initialInvestment').value);
const discountRate = parseFloat(document.getElementById('discountRate').value) / 100;
const cashFlowItems = document.querySelectorAll('.cash-flow-item input');

if (isNaN(initialInvestment) || isNaN(discountRate)) {
alert('Please enter valid inputs.');
return;
}

let npv = -initialInvestment;
cashFlowItems.forEach((input, index) => {
const cashFlow = parseFloat(input.value);
if (isNaN(cashFlow)) {
alert('Please enter valid cash flow values.');
return;
}
npv += cashFlow / Math.pow(1 + discountRate, index + 1);
});

document.getElementById('npvResult').textContent = npv.toFixed(2);
}
71 changes: 71 additions & 0 deletions Calculators/NPV-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(to right, #f48a8a, #a3dbf5);
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.calculator-container {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 20px;
width: 500px;
text-align: center;
}

h1 {
color: #333;
}

.input-container {
margin-bottom: 15px;
}

label {
display: block;
margin-bottom: 5px;
color: #555;
}

input {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box;
}

button {
background-color: #4caf50;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}

button:hover {
background-color: #45a049;
transform: scale(1.05);
transition: 0.2s ease-in-out;
}

.result-container {
margin-top: 20px;
}

#result {
font-size: 18px;
font-family: 'Courier New', Courier, monospace;
font-weight: bold;
color: #333;
}

.red {
color: #f44336;
}
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,20 @@ <h3>Calculates the multiplication table of any number.</h3>
</a>
</div>
</div>
<div class="box">
<div class="content">
<h2>NPV Calculator</h2>
<h3>Calculates the Net Present Value based on initial investment, the discount rate, and the yearly cash flows.</h3>
<div class="card-footer">
<a href="./Calculators/NPV-Calculator/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/NPV-Calculator/" title="Source Code" target="_blank">
<img src="./assets/images/github.png" alt="Source Code"></img>
</a>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Neon Number Calculator</h2>
Expand Down

0 comments on commit a4eb8c8

Please sign in to comment.