0% found this document useful (0 votes)
10 views7 pages

Javascript Programs

The document contains multiple JavaScript programs demonstrating basic operations such as addition of two numbers, finding the greatest of three numbers, and performing arithmetic operations using prompts and functions. Each section includes HTML code for user input and JavaScript for processing the input and displaying results. Additionally, there is a simple demonstration of using an external JavaScript file to show an alert message.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Javascript Programs

The document contains multiple JavaScript programs demonstrating basic operations such as addition of two numbers, finding the greatest of three numbers, and performing arithmetic operations using prompts and functions. Each section includes HTML code for user input and JavaScript for processing the input and displaying results. Additionally, there is a simple demonstration of using an external JavaScript file to show an alert message.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Write a javascript program to find the addition of two numbers:

Using prompt:

<html>

<head>

<script>

var num1 = parseFloat(prompt("Enter first number:"));

var num2 = parseFloat(prompt("Enter second number:"));

var sum = num1 + num2;

// Displaying the result

[Link](`The sum of ${num1} and ${num2} is ${sum}`);

alert(`The sum of ${num1} and ${num2} is ${sum}`);

</script>

</head>

</html>

Using function

<html>

<head>

<title>Addition</title>

</head>

<body>

<h2>Add Two Numbers</h2>

<input type="number" id="num1" placeholder="Enter first number">

<input type="number" id="num2" placeholder="Enter second number">


<button onclick="add()">Add</button>

<p id="result"></p>

<script>

function add() {

var n1 = parseFloat([Link]("num1").value);

var n2 = parseFloat([Link]("num2").value);

var sum = n1 + n2;

alert(sum);

</script>

</body>

</html>

2. Write a javascript program to find the greates of three numbers

<html>

<head>

<title>greatest</title>

</head>

<body>

<script>

let num1 = parseFloat(prompt("Enter first number:"));

let num2 = parseFloat(prompt("Enter second number:"));

let num3 = parseFloat(prompt("Enter third number:"));

let max = [Link](num1, num2, num3);

[Link](`The greatest number is ${max}`);

alert(`The greatest number is ${max}`);


</script>

</body>

</html>

Using functions

<html>

<head>

<title>Addition</title>

<title>Greatest of Three Numbers</title>

</head>

<body>

<h2>Find the Greatest Number</h2>

<input type="number" id="num1" placeholder="Enter first number">

<input type="number" id="num2" placeholder="Enter second number">

<input type="number" id="num3" placeholder="Enter third number">

<button onclick="findMax()">Find Greatest</button>

<p id="result"></p>

<script>

function findMax() {

let n1 = parseFloat([Link]("num1").value);

let n2 = parseFloat([Link]("num2").value);

let n3 = parseFloat([Link]("num3").value);

let max = [Link](n1, n2, n3);

[Link]("result").innerText = "Greatest Number: " + max;


}

</script>

</body>

</html>

3. Write a Javascript program to perform arithmetic operations.

<html>
<head>
<title>Javacript</title>
<title>arithmetic operations</title>
</head>
<body>
<script>
let num1 = parseFloat(prompt("Enter first number:"));
let num2 = parseFloat(prompt("Enter second number:"));
let operator = prompt("Enter operator (+, -, *, /):");
let result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 !== 0) {
result = num1 / num2;
} else {
result = "Error! Division by zero.";
}
break;
default:
result = "Invalid operator!";
}

// Displaying the result


[Link](`Result: ${result}`);
alert(`Result: ${result}`);

</script>
</body>
</html>

<html>
<head>
<title>Addition</title>
<title>Greatest of Three Numbers</title>
<title>Calculator using Switch Case</title>
</head>
<body>
<h2>Simple Calculator</h2>
<input type="number" id="num1" placeholder="Enter first number">
<input type="number" id="num2" placeholder="Enter second number">
<select id="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<button onclick="calculate()">Calculate</button>
<p id="result"></p>

<script>
function calculate() {
let n1 = parseFloat([Link]("num1").value);
let n2 = parseFloat([Link]("num2").value);
let op = [Link]("operator").value;
let res;
switch (op) {
case '+':
res = n1 + n2;
break;
case '-':
res = n1 - n2;
break;
case '*':
res = n1 * n2;
break;
case '/':
res = n2 !== 0 ? n1 / n2 : "Error! Division by zero.";
break;
default:
res = "Invalid operator!";
}

[Link]("result").innerText = "Result: " + res;


}
</script>
</body>
</html>

4. Javacript
[Link]
<html>
<head>
<script type="text/javascript" src="[Link]"></script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
[Link]
function msg(){
alert("Hello world");
}

You might also like