8770670-Class8 Javascript Worksheet
8770670-Class8 Javascript Worksheet
Solutions
1)Write a JavaScript program to swap the value of two variables without using
a third variable. Take the value of both variables from the user.
<html>
<head>
<title>Javascript</title></head>
<body>
<script type="text/javascript">
var x=parseInt(prompt("Enter first number"));
var y=parseInt(prompt("Enter second number"));
x = x+y ;
y = x -y ;
x = x -y ;
document.write("x =");
document.write(x);
document.write("<br>");
1| 7- 0 5 - 2 0 2 3 / P R E P A R E D B Y : T e s s M a r y T h o m a s / I C T D e p t .
document.write("y =");
document.write(y);
</script>
</body>
</html>
2) Write a Javascript program to calculate the area of a square
<html>
<head>
<title>Area of square</title></head>
<body>
<script type="text/javascript">
var s=parseInt(prompt("enter the side of square"));
var area;
area=s*s;
document.write("Area of square=");
document.write(area);
</script>
</body></html>
3)Write a Javascript program to convert Kilometers to centimetrs
<html>
<head>
<title>javascript</title></head>
<body>
<script type="text/javascript">
var km=parseInt(prompt("enter the value in Kilometer"));
var cm;
cm=100000*km;
document.write("kilometre to centimetre=");
document.write(cm);
</script>
</body>
</html>
2| 7- 0 5 - 2 0 2 3 / P R E P A R E D B Y : T e s s M a r y T h o m a s / I C T D e p t .
4 )Develop a JavaScript program to perform following arithmetic operations:
addition, subtraction, multiplication and division.
<html>
<head>
<title>Arithmetic operators in Javascript</title></head>
<body>
<script type="text/javascript">
var a=parseInt(prompt("enter first number"));
var b=parseInt(prompt("enter second number"));
var sum;
var difference;
var product;
var quotient;
sum=a+b;
difference=a-b;
product=a*b;
Quotient=a/b;
document.write("Sum= ");
document.write(sum);
document.write("<br>");
document.write("Difference =");
document.write(difference);
document.write("<br>");
document.write("Product=");
document.write(product);
document.write("<br>");
document.write("Quotient=");
document.write(quotient);
</script>
</body>
</html>
3| 7- 0 5 - 2 0 2 3 / P R E P A R E D B Y : T e s s M a r y T h o m a s / I C T D e p t .