maths
Fri Nov 01 2024 13:58:15 GMT+0000 (Coordinated Universal Time)
Saved by @abhigna
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Pop-Up Examples</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
button {
margin: 10px 0;
padding: 10px 15px;
font-size: 16px;
}
input[type="text"] {
margin-top: 10px;
width: 200px;
padding: 10px;
}
</style>
</head>
<body>
<h1>JavaScript Pop-Up Examples</h1>
<h2>Display Current Date</h2>
<button onclick="displayDate()">Display Date</button>
<input type="text" id="dateOutput" placeholder="Date will be displayed here" readonly>
<!-- b) Factorial -->
<h2>Calculate Factorial</h2>
<button onclick="calculateFactorial()">Calculate Factorial</button>
<!-- c) Multiplication Table -->
<h2>Multiplication Table</h2>
<button onclick="multiplicationTable()">Show Multiplication Table</button>
<script>
// Function a: Display current date in the textbox
function displayDate() {
const date = new Date();
document.getElementById('dateOutput').value = date.toLocaleString();
}
// Function b: Calculate factorial of a number
function calculateFactorial() {
const n = prompt("Enter a number to calculate its factorial:");
if (n === null || n === "" || isNaN(n) || n < 0) {
alert("Please enter a valid non-negative number.");
return;
}
let factorial = 1;
for (let i = 1; i <= n; i++) {
factorial *= i;
}
alert(`Factorial of ${n} is ${factorial}`);
}
// Function c: Show multiplication table of a number
function multiplicationTable() {
const n = prompt("Enter a number to see its multiplication table:");
if (n === null || n === "" || isNaN(n)) {
alert("Please enter a valid number.");
return;
}
let table = `Multiplication Table of ${n}:\n`;
for (let i = 1; i <= 10; i++) {
table += `${n} x ${i} = ${n * i}\n`;
}
alert(table);
}
</script>
</body>
</html>



Comments