You are on page 1of 4

1) Write a JavaScript program to determine whether a given year is a leap year

in the Gregorian calendar.

2) Write a JavaScript program where the program takes a random integer


between 1 to 10, the user is then prompted to input a guess number. If the user
input matches with guess number, the program will display a message "Good
Work" otherwise display a message "Not matched".

3) Write a JavaScript program to calculate multiplication and division of two


numbers (input from user).

4) Write a JavaScript program to convert temperatures to and from Celsius,


Fahrenheit.
[Formula: c/5 = (f-32)/9 [ where c = temperature in Celsius and f = temperature
in Fahrenheit]
Expected Output:
60°C is 140 °F
45°F is 7.222222222222222°C
Solution of Problem 1:
HTML:
<html>
<head>
<script src="extra.js"></script>
</head>
<body>
Type any Year:<input type="text" id="t1" size="5"><br>
<button onclick="ly()">Show</button>
<p id="a"> </p>
</body>
</html>

Extra.js:
function ly(){
var x=parseInt(document.getElementById("t1").value);
document.getElementById("a").innerHTML="";
if(x%4==0){
if(x%100==0){
if(x%400==0){
document.getElementById("a").innerHTML=x+" is a Leap
Year"; }
else
document.getElementById("a").innerHTML=x+" is NOT a Leap
Year"; }
else {
document.getElementById("a").innerHTML=x+" is a Leap
Year"; }
}
else
document.getElementById("a").innerHTML=x+" is NOT a Leap
Year"; }
Solution of Problem 2:
HTML:
<html>
<head>
<script src="extra.js"></script>
</head>
<body>
Guess a Number between 1 to 10:<input type="text" id="t1" size="5"><br>
<button onclick="num()">Show</button>
<p id="a"> </p>
</body>
</html>

Extra.js:
function num(){
var x=parseInt(document.getElementById("t1").value);
document.getElementById("a").innerHTML="";
var y = Math.ceil(Math.random() * 10);
if(x==y) document.getElementById("a").innerHTML="Good Work";
else document.getElementById("a").innerHTML="Not matched"; }

Solution of Problem 3:
HTML:
<html>
<head>
<script src="extra.js"></script>
</head>
<body>
1st Number:<input type="text" id="t1" size="5"><br>
2nd Number:<input type="text" id="t2" size="5"><br>
<button onclick="mul()">Multiply</button>
<button onclick="div()">Divide</button>
<p id="a"> </p>
</body>
</html>
Extra.js:
function mul(){
var x=parseInt(document.getElementById("t1").value);
var y=parseInt(document.getElementById("t2").value);
document.getElementById("a").innerHTML=
"The Result is "+x*y;
}
function div(){
var x=parseInt(document.getElementById("t1").value);
var y=parseInt(document.getElementById("t2").value);
document.getElementById("a").innerHTML=
"The Result is "+x/y;
}

Solution of Problem 4:
HTML:
<html>
<head>
<script src="extra.js"></script>
</head>
<body>
Type Celsius OR Fahrenheit:<input type="text" id="t1" size="5"><br>
<button onclick="c2f()">C 2 F</button>
<button onclick="f2c()">F 2 C</button>
<p id="a"> </p>
</body>
</html>

Extra.js:
function c2f(){
var c=parseInt(document.getElementById("t1").value);
var f=(c/5*9)+32;
document.getElementById("a").innerHTML=
c+"<sup>0</sup>C is "+ f+"<sup>0</sup>F"; }
function f2c(){
var f=parseInt(document.getElementById("t1").value);
var c=(f-32)/9*5;
document.getElementById("a").innerHTML=
f+"<sup>0</sup>F is "+ c+"<sup>0</sup>C"; }

You might also like