-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Net Present Value Calculator (#797)
- Loading branch information
Showing
5 changed files
with
190 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,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) |
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,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> |
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,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); | ||
} |
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,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; | ||
} |
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