-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Catalan Number Calculator (#1393)
- Loading branch information
1 parent
5e2f155
commit c743df6
Showing
5 changed files
with
147 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,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) |
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,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> |
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,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]; | ||
} |
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,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; | ||
} |
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