Skip to content

Commit

Permalink
Added Catalan Number Calculator (#1393)
Browse files Browse the repository at this point in the history
  • Loading branch information
Will2Jacks authored Jun 24, 2024
1 parent 5e2f155 commit c743df6
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Calculators/Catalan-Number-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# <p align="center">Catalan Number Calculator</p>

## Description :-

Calculates the n<sup>th</sup> Catalan Number with the value of n entered by the user.

## Tech Stacks :-

- HTML
- CSS
- JavaScript

## Screenshots :-

![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/67259e46-ea5c-4559-8e44-9a8c4ca60703)
25 changes: 25 additions & 0 deletions Calculators/Catalan-Number-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!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>Catalan Number Calculator</title>
</head>
<body>
<header>
<h1>Nth Catalan Number Calculator</h1>
</header>
<main>
<div class="container">
<form name="myform">
<label for="no">Enter the term you want to find:</label>
<input type="number" name="term" id="no">
<button type="button" onclick="findCatalanNumber()">Find Catalan Number</button>
</form>
<div id="catalanNumbers"></div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions Calculators/Catalan-Number-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fact = []
fact.push(1)
for (let i = 1; i <= 100; i++) {
fact.push(fact[i - 1] * i);
}

function findCatalanNumber() {
const term = document.forms.myform.term.value;
const n = parseInt(term, 10);
//Validate User Input
if (n === "" || n < 0 || n === 0 || n > 32 || Number.isNaN(n)) {
alert('Please enter a valid number between 1 and 32');
} else {
myElement = document.getElementById("catalanNumbers");
const num = catalanNumber(n);
const st = num.toString();
if (n % 10 === 1 && n != 11) {
myElement.textContent = "The value of the " + n.toString() + "st Catalan Number is " + st;
} else if (n % 10 === 2 && n != 12) {
myElement.textContent = "The value of the " + n.toString() + "nd Catalan Number is " + st;
} else if (n % 10 === 3 && n != 13) {
myElement.textContent = "The value of the " + n.toString() + "rd Catalan Number is " + st;
} else {
myElement.textContent = "The value of the " + n.toString() + "th Catalan Number is " + st;
}
}
}

function catalanNumber(val) {
let numerator = factorial(2 * val);
let firstDenominator = factorial(val + 1);
let secondDenominator = factorial(val);
let denominator = firstDenominator * secondDenominator;
return Math.floor(numerator / denominator);
}

function factorial(j) {
return fact[j];
}
54 changes: 54 additions & 0 deletions Calculators/Catalan-Number-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
body {
background-color: #f1ffb1;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}

h1 {
color: #8C1C1C;
margin-bottom: 40px;
}

.container {
display: flex;
flex-direction: column;
color: rgb(78, 2, 78);
font-size: large;
align-items: center;
justify-content: center;
background-color: white;
border-radius: 30px;
padding: 100px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

form {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20px;
}

label {
margin-bottom: 20px;
font-weight: bold;
}

input {
padding: 8px;
margin-bottom: 20px;
width: 150px;
}

button {
padding: 10px;
background-color: red;
color: white;
border: none;
cursor: pointer;
font-size: medium;
}
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,20 @@ <h3>Calculates an individual's carbon footprint based on some input parameters.<
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Catalan Number Calculator</h2>
<h3>Calculates the nth Catalan Number with the value of n entered by the user.</h3>
<div class="card-footer">
<a href="./Calculators/Catalan-Number-Calculator/index.html" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Catalan-Number-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>Centripetal Force Calculator</h2>
Expand Down

0 comments on commit c743df6

Please sign in to comment.