1. Add of two numbers
Method 1:
<html>
<head>
<title>Adding two numbers using JavaScript in HTML</title>
<script>
var num1 = 10;
var num2 = 40;
var sum = num1+num2;
document.write("Sum of two numbers is " + sum);
</script>
</head>
<body></body>
</html>
Method 2:
<html>
<head>
<script>
function add()
{
var num1, num2, sum;
num1 = parseInt(document.getElementById("firstnumber").value);
num2 = parseInt(document.getElementById("secondnumber").value);
sum = num1 + num2;
document.getElementById("answer").value = sum;
}
</script>
</head>
<body>
<p>First Number: <input id="firstnumber"></p>
<p>Second Number: <input id="secondnumber"></p>
<button onclick="add()">Add Them</button>
<p>Sum = <input id="answer"></p>
</body>
</html>
Method 3:
<!doctype html>
<html>
<head>
<script>
var num1, num2, sum;
function FirstNum()
{
num1 = parseInt(document.getElementById("firstNumber").value);
if(num1)
{
temp = document.getElementById("blockOne");
temp.style.display = "none";
temp = document.getElementById("blockTwo");
temp.style.display = "block";
}
}
function SecondNum()
{
num2 = parseInt(document.getElementById("secondNumber").value);
if(num1 && num2)
{
temp = document.getElementById("blockOne");
temp.style.display = "none";
temp = document.getElementById("blockTwo");
temp.style.display = "none";
sum = num1 + num2;
temp = document.getElementById("blockThree");
temp.style.display = "block";
document.getElementById("res").innerHTML = sum;
}
}
</script>
</head>
<body>
<p id="blockOne"> First Number <input id="firstNumber">
<button onclick="FirstNum()">Enter</button>
</p>
<p id="blockTwo" style="display:none;">Second Number <input id="secondNumber">
<button onclick="SecondNum()">Add</button>
</p>
<p id="blockThree" style="display:none;">Sum = <span id="res"></span></p>
</body>
</html>
2. Largest of three numbers
<!doctype html>
<html>
<head>
<script>
function largestnum()
{
var num1, num2, num3;
num1 = parseInt(document.getElementById("first").value);
num2 = parseInt(document.getElementById("second").value);
num3 = parseInt(document.getElementById("third").value);
if(num1>num2 && num1>num3)
{
window.alert(num1+ "is greatest");
}
else if(num2>num1 && num2>num3)
{
window.alert(num2+ "is greatst");
}
else if(num3>num1 && num3>num1)
{
window.alert(num3+ "is greatest");
}
}
</script>
</head>
<body>
<h1>Check largest number </h1>
<br>
Enter number 1: <input type="text" id="first"></input><br>
Enter number 2: <input type="text" id="second"></input><br>
Enter number 3: <input type="text" id="third"></input><br>
<button onclick="largestnum()">submit</button>
</body>
</html>
3. Odd or Even
Method 1:
<!doctype html>
<html>
<body>
<script>
var num=6;
if(num%2==0)
document.write(num + " is an Even Number");
else
document.write(num + " is an Odd Number");
</script>
</body>
</html>
Method 2:
<!doctype html>
No comments:
Post a Comment