File 5
File 5
Enter a list of positive numbers using the prompt terminated by a zero. Find the
sum and average of these numbers.
Ans. 1. Enter a number and on click of a button, print its multiplication table.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplication Table</title>
</head>
<body>
<h2>Multiplication Table</h2>
<input type="number" id="numberInput" placeholder="Enter a number">
<button onclick="generateTable()">Generate Table</button>
<div id="tableOutput"></div>
<script src="script.js"></script>
</body>
</html>
JavaScript (script.js):
function generateTable() {
const number = document.getElementById("numberInput").value;
const outputDiv = document.getElementById("tableOutput");
if (number) {
let table = "<h3>Multiplication Table for " + number + ":</h3><ul>";
for (let i = 1; i <= 10; i++) {
table += "<li>" + number + " x " + i + " = " + (number * i) + "</li>";
}
table += "</ul>";
outputDiv.innerHTML = table;
} else {
outputDiv.innerHTML = "<p>Please enter a valid number.</p>";
}
}
Explanation:
HTML: The user inputs a number, and when the button is clicked, the multiplication
table is generated and displayed in the <div> with the ID tableOutput.
JavaScript: The generateTable function fetches the input value and generates the
multiplication table up to 10 times, displaying it on the page.
---
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Largest Number</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
JavaScript (script.js):
function findLargest() {
const num1 = document.getElementById("num1").value;
const num2 = document.getElementById("num2").value;
const num3 = document.getElementById("num3").value;
const largestOutput = document.getElementById("largestOutput");
Explanation:
HTML: There are three input fields for the user to enter the numbers. A button
triggers the findLargest function.
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Factorial Calculator</title>
</head>
<body>
<h2>Factorial Calculator</h2>
<input type="number" id="factorialInput" placeholder="Enter a number">
<button onclick="calculateFactorial()">Calculate Factorial</button>
<div id="factorialOutput"></div>
<script src="script.js"></script>
</body>
</html>
JavaScript (script.js):
function calculateFactorial() {
const num = document.getElementById("factorialInput").value;
const factorialOutput = document.getElementById("factorialOutput");
Explanation:
HTML: The user enters a number, and when they click the button, the factorial of
that number is calculated and displayed in the output <div>.
---
4. Enter a list of positive numbers using the prompt terminated by a zero. Find the
sum and average of these numbers.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sum and Average Calculator</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
JavaScript (script.js):
function calculateSumAndAverage() {
let sum = 0;
let count = 0;
let number;
while (true) {
number = prompt("Enter a positive number (enter 0 to stop):");
number = parseFloat(number);
if (number === 0) {
break;
} else if (number > 0) {
sum += number;
count++;
} else {
alert("Please enter a valid positive number.");
}
}
if (count > 0) {
const average = sum / count;
document.getElementById("sumAvgOutput").innerHTML =
<h3>Sum: ${sum}</h3><h3>Average: ${average}</h3>;
} else {
document.getElementById("sumAvgOutput").innerHTML = "<p>No valid numbers
entered.</p>";
}
}
Explanation:
HTML: The user clicks the button to enter a series of numbers using the prompt. The
process stops when the user enters 0.