Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Javascript Programs with Solution

The document contains a series of event-driven JavaScript programs that demonstrate various functionalities such as displaying odd/even numbers, calculating square roots, checking for prime numbers, and more. Each program includes HTML structure and JavaScript code to handle user interactions and display results. The programs cover a range of topics suitable for learning basic JavaScript and event handling.

Uploaded by

casetooyt787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Javascript Programs with Solution

The document contains a series of event-driven JavaScript programs that demonstrate various functionalities such as displaying odd/even numbers, calculating square roots, checking for prime numbers, and more. Each program includes HTML structure and JavaScript code to handle user interactions and display results. The programs cover a range of topics suitable for learning basic JavaScript and event handling.

Uploaded by

casetooyt787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Javascript Programs with Solution:-

1) Write an event driven javascript program to display odd numbers between 50


to 100.

Solution:-

<html>
<head>
<title>Odd Numbers between 50 to 100</title>
</head>
<body>
<h3 align = "center">To display odd numbers between 50 to 100, click on this
button.</h3>
<form name = "f1">
<input type = "button" value = "Display Numbers" name = "b1" onClick =
"display()">
</form>
<script type = "text/javascript">
function display()
{
var i;
for(i=50;i<=100;i++)
{
if(i%2!= 0)
{
document.write(i + "<br>");
}
}
}
</script>
</body>
</html>

2) Write an event driven javascript program to display square root and cube root
of a number using built-in function of math object.

Solution:-

<html>
<head>
<title>Display Square root and cube root</title>
</head>
<body>
<h1 align = "center">Display Square root and cuberoot using Built-in
Object</h1>
<form name = "f1">
Enter the number:-<input type = "number" name = "t1"><br><br>
<input type = "button" name = "b1" value = "Display" onClick = "calc()">
</form>
<script type = "text/javascript">
function calc()
{
var a;
a = f1.t1.value;
document.write("Square root of number is " + Math.sqrt(a));
document.write("<br><br>Cube root of number is " + Math.cbrt(a));
}
</script>
</body>
</html>

3) Write an event driven javascript program to display factorial of 7 when the


mouse is moved on button.

Solution:-

<html>
<head>
<title>Factorial of a number</title>
</head>
<body>
<h1 align = "center">Display the factorial of 7</h1>
<form>
<input type = "button" name = "b1" value = "Display Factorial"
onMouseOver = "fact()">
</form>
<script type = "text/javascript">
function fact()
{
var i,a=7,f=1;
for(i=1;i<=a;i++)
{
f=f*i;
}
document.write("Factorial of 7 is " + f);
}
</script>
</body>
</html>

4) Write an event driven javascript program to display square of numbers from 2


to 10 on button click.

Solution:-

<html>
<head>
<title>Square of numbers from 2 to 10</title>
</head>
<body>
<h1 align = "center">To display Square of numbers from 2 to 10,
Click on Button</h1>
<form name = "f1">
<input type = "button" value = "Display square" name = "b1"
onClick = "displaysqu()">
</form>
<script type = "text/javascript">
function displaysqu()
{
var i,squ;
for(i=2; i<=10; i++)
{
squ = i*i;
document.write("<br><br>Square of " + i + " is " + squ);
}
}
</script>
</body>
</html>
5) Write an event driven javascript program to display 2 colors after every 3000
milliseconds on click of a button.

Solution:-

<html>
<head>
<title>Event-driven Program</title>
</head>
<body>
<form name = "form1">
<input type = "button" name = "b1" value = "change color"
onClick = "f1()">
</form>
<script type = "text/javascript">
function f1()
{
document.bgColor = "pink";
window.setTimeout("f2()", 3000);
}
function f2()
{
document.bgColor = "skyblue";
window.setTimeout("f1()", 3000);
}
</script>
</body>
</html>

6) Write event driven Javascript program to check whether the number is prime
number or not.

Solution:-

<html>
<head>
<title>prime Numbers</title>
</head>
<body>
<form name = "f1">
Enter the Number : <input type = "number" name = "t1"><br><br>
<input type = "button" name = "b1" Value = "Check" onClick = "checkprime()">
</form>
<script type = "text/javascript">
function checkprime()
{
var i,a,p;
a=f1.t1.value;
p=1;
for(i=2; i<a; i++)
{
if(a%i == 0)
{
p=0;
break;
}
}
if(p == 1)
document.write("Number is Prime Numbers");
else
document.write("Number is not Prime Numbers");
}
</script>
</body>
</html>

7) Write an event driven javascript program to accept number from user and
check whether it is less than 150, equal to 150 and greater than 150.

Solution:-

<html>
<head>
<title>Number Equality</title>
</head>
<body>
<form name = "f1">
Enter the number:-
<input type = "number" name = "n1"><br><br>
<input type = "button" name = "b1" value = "Dispaly Result"
onClick = "findresult()">
<script type = "text/javascript">
function findresult()
{
var a;
a = f1.n1.value;
if(a<150)
{
document.write("Given number is less than 150");
}
else if(a>150)
{
document.write("Given number is greater than 150");
}
else
{
document.write("Given number is equal to 150");
}
}
</script>
</body>
</html>

8) Write an event driven javascript program to display number sequence from


100 and 250 when the mouse is moved over a button.

Solution:-

<html>
<head>
<title>Number Sequence from 100 to 250</title>
</head>
<body>
<h1 align = "center">Number Sequence from 100 to 250</h1>
<form>
<input type = "button" value = "Display Sequence" name = "b1"
onMouseOver = "display()">
</form>
<script type = "text/javascript">
function display()
{
var i;
document.write("Number sequence is:- <br>");
for(i=100;i<=250;i++)
{
document.write(i + "<br>");
}
}
</script>
</body>
</html>

9) Write an event driven program to calculate length of the string. Accept the
string from user.

Solution:-

<html>
<head>
<title>String length</title>
</head>
<body>
<h1 align = "center">String lenth</h1>
<form name = "f1">
Enter the string:-
<input type = "text" name = "t1"><br><br>
<input type = "submit" name = "b1" value = "calculate length"
onClick = "cal()">
</form>
<script type = "text/javascript">
function cal()
{
var a,b;
a = f1.t1.value;
b = a.length;
document.write("Length of given string is "+b);
}
</script>
</body>
</html>
10) Write a Javascript program to display even numbers from 1 to 50.

Solution:-
<html>
<head>
<title>Even Number from 1 to 50</title>
<script language="javascript">
var i;
document.write("<b>" + "Even numbers from 1 to 50 are as follows:-" +
"<br><br>");
for(i=1;i<=50;i++)
{
if(i%2==0)
{
document.write(i + "<br>");
}
}
</script>
</head>
</html>

11) Write a Javascript program to display odd numbers from 1 to 50.


Solution:-
<html>
<head>
<title>Odd Number from 1 to 50</title>
<script language="javascript">
var i;
document.write("<b>" + "Odd numbers from 1 to 50 are as follows:-" +
"<br><br>");
for(i=1;i<=50;i++)
{
if(i%2!=0)
{
document.write(i + "<br>");
}
}
</script>
</head></html>
12) Write a Javascript program to display sum of even numbers from 1 to 20.
Solution:-
<html>
<head>
<title>Sum of Even Number from 1 to 20</title>
<script language="javascript">
var i,sum=0;
document.write("Even numbers from 1 to 20 are as follows:-" + "<br><br>");
for(i=1;i<=20;i++)
{
if(i%2==0)
{
document.write(i + "<br>");
sum=sum+i;
}
}
document.write("<br>Sum of even numbers between 1 to 20 is" + " " + sum);
</script>
</head>
</html>

13) Write a Javascript program to display sum of odd numbers from 1 to 20.
Solution:-
<html>
<head>
<title>Sum of Odd Number from 1 to 20</title>
<script language="javascript">
var i,sum=0;
document.write("Odd numbers from 1 to 20 are as follows:-" + "<br><br>");
for(i=1;i<=20;i++)
{
if(i%2!=0)
{
document.write(i + "<br>");
sum=sum+i;
}
}
document.write("<br>Sum of odd numbers between 1 to 20 is" + " " + sum);
</script>
</head>
</html>

14) Write event driven Javascript program to check whether the entered number is
even or odd.
Solution:-

<html>
<head>
<title>Even Or Odd Number</title>
</head>
<body>
<form name = "f1">
Enter the Number : <input type = "text" name = "t1"><br><br>
<input type = "button" name = "b1" Value = "Check" onClick = "checknumber()">
</form>
<script type = "text/javascript">
function checknumber()
{
var i,a,p;
a=f1.t1.value;
if(a%2 == 0)
{
document.write("Number is Even");
}
else
{
document.write("Number is Odd");
}
}
</script>
</body>
</html>
15) Write an event driven program to display perimeter of square. Accept the side
from the user. (perimeter = 2*side*side)
Solution:-

<html>
<head>
<title>Square calculations</title>
</head>
<body>
<h1 align = "center">Calculate the Perimeter of square</h1>
<form name = "f1">
Enter the side:-
<input type = "number" name = "n1"><br><br>
<input type = "button" name = "b1" value = "Calculate"
onClick = "calculate()">
</form>
<script type = "text/javascript">
function calculate()
{
var a,result;
a = f1.n1.value;
result = 2*a*a;
document.write("Perimeter of square is " + result);
}
</script>
</body>
</html>

You might also like