0% found this document useful (0 votes)
12 views1 page

Experiment No - 8

Uploaded by

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

Experiment No - 8

Uploaded by

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

Experiment No: 8

//Title:-Programs on Java Script User Defined Functions.

Source Code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Call JavaScript Function in HTML</title>
<script>
// JavaScript user-defined function to add two numbers
function addNumbers(num1, num2)
{
// Calculate the sum of the two numbers
let sum = num1 + num2;
// Return the result
return sum;
}
// Function to get input from the user and display the result
function displaySum()
{
// Get values from input fields
let number1 = parseFloat(document.getElementById("num1").value);
let number2 = parseFloat(document.getElementById("num2").value);
// Call addNumbers() function and get the result
let result = addNumbers(number1, number2);
// Display the result in the <p> element
document.getElementById("result").innerHTML = "The sum is: " + result;
}
</script>
</head>
<body>
<h1>Add Two Numbers</h1>
<!-- Input fields to take user input -->
<label for="num1">Number 1:</label>
<input type="text" id="num1" placeholder="Enter first number">
<br><br>
<label for="num2">Number 2:</label>
<input type="text" id="num2" placeholder="Enter second number">
<br><br>
<!-- Button to call the function -->
<button onclick="displaySum()">Add Numbers</button>
<!-- Paragraph to display the result -->
<p id="result"></p>
</body>
</html>

Output:-

You might also like