0% found this document useful (0 votes)
12 views2 pages

Pratical 7

This document contains an event-driven JavaScript program with four functions: printMultiplicationTable prints the multiplication table of a number, printLargestNumbers prints the three largest numbers entered, calculateFactorial calculates the factorial of a number, and calculateSumAndAverage prompts the user to enter numbers and calculates the sum and average. Each function is triggered by the onclick event of a button and outputs the result to the console.

Uploaded by

piyush120805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Pratical 7

This document contains an event-driven JavaScript program with four functions: printMultiplicationTable prints the multiplication table of a number, printLargestNumbers prints the three largest numbers entered, calculateFactorial calculates the factorial of a number, and calculateSumAndAverage prompts the user to enter numbers and calculates the sum and average. Each function is triggered by the onclick event of a button and outputs the result to the console.

Uploaded by

piyush120805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

<!

DOCTYPE html>
<html>
<head>
<title>Event-Driven JavaScript Program</title>
</head>
<body>
<h1>Event-Driven JavaScript Program</h1>

<h2>Multiplication Table</h2>
<input type="number" id="numberInput">
<button onclick="printMultiplicationTable()">Print Table</button>

<h2>Largest 3 Numbers</h2>
<input type="number" id="number1">
<input type="number" id="number2">
<input type="number" id="number3">
<button onclick="printLargestNumbers()">Print Largest</button>

<h2>Factorial</h2>
<input type="number" id="factorialInput">
<button onclick="calculateFactorial()">Calculate Factorial</button>

<h2>Sum and Average</h2>


<button onclick="calculateSumAndAverage()">Enter Numbers</button>

<script>
function printMultiplicationTable() {
var number = document.getElementById("numberInput").value;
for (var i = 1; i <= 10; i++) {
var result = number * i;
console.log(number + " x " + i + " = " + result);
}
}

function printLargestNumbers() {
var number1 = parseInt(document.getElementById("number1").value);
var number2 = parseInt(document.getElementById("number2").value);
var number3 = parseInt(document.getElementById("number3").value);
var numbers = [number1, number2, number3];
numbers.sort(function(a, b) {
return b - a;
});
console.log("Largest 3 numbers: " + numbers.slice(0, 3));
}

function calculateFactorial() {
var number = parseInt(document.getElementById("factorialInput").value);
var factorial = 1;
for (var i = 1; i <= number; i++) {
factorial *= i;
}
console.log("Factorial of " + number + " is " + factorial);
}

function calculateSumAndAverage() {
var numbers = [];
var input = prompt("Enter a positive number (enter 0 to terminate):");
while (input != 0) {
numbers.push(parseInt(input));
input = prompt("Enter a positive number (enter 0 to terminate):");
}
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
var average = sum / numbers.length;
console.log("Sum: " + sum);
console.log("Average: " + average);
}
</script>
</body>
</html>

You might also like