JavaScript Programs
JavaScript Programs
3. Write event driven program in JavaScript to accept radius from user and display
the area of circle.
<html>
<head>
<script language="JavaScript">
function circle()
{
var r;
r=parseFloat(document.f1.r1.value);
document.write("Area of Circle = ",3.14*r*r);
}
</script>
</head>
<body><form name=f1>
Enter Radius:<input type=text name=r1><p>
<input type="button" name="b1" value="Find Area" onClick ="circle()">
</form>
</body>
</html>
7. Write JavaScript Event Driven program to count length of given string after
clicking on button.
<html>
<head>
<script language="JavaScript">
function len()
{
var a;
a=document.f1.t1.value;
document.write("Length of string = ",a.length);
}
</script>
</head>
<body>
<form name=f1>
Enter String: <input name=t1><br>
<input type="button" name="b1" value="Get Length" onClick ="len()">
</form>
</body>
</html>
8. Write JavaScript code to accept any number and checked the number is divisible
by 5 & 7 after mouse button is clicked.
<html>
<head>
<script language="JavaScript">
function div()
{
var a;
a=parseInt(document.f1.t1.value);
if(a%5==0 && a%7==0)
{ document.write("Number is divisible."); }
else
{ document.write("Number is not divisible."); }
}
</script>
</head>
<body>
<form name=f1>
Enter Number:<input type=text name=t1><p>
<input type="button" name="b1" value="Divisible nos." onClick ="div()">
</form>
</body>
</html>
9. Write event driven JavaScript program to find greater number from three
accepted numbers.
<html>
<head>
<script language ="JavaScript">
function great()
{
var a,b,c;
a=parseInt(document.f1.n1.value);
b= parseInt(document.f1.n2.value);
c= parseInt(document.f1.n3.value);
document.write("Greater number is ");
if(a>=b && a>=c)
{ document.write(a); }
else
{
if(b>=a && b>=c)
{ document.write(b); }
else
{ document.write(c); }
}
}
</script>
</head>
<body>
<form name=f1>
Enter 1st no :<input type="text" name="n1"> <br>
Enter 2nd no :<input type="text" name="n2"> <br>
Enter 3rd no :<input type="text" name="n3"> <br>
<input type="button" name="button1" value="Check" onClick = " great()">
</form></body></html>
11.Write JavaScript code to print all the numbers from 10 to 50 on click event.
<html>
<head>
<script language="JavaScript">
function nos()
{
var i;
document.write("Nos. from 10 to 50 are : <br>");
for(i=10;i<=50;i++)
{ document.write("<br>"+i); }
}
</script></head>
<body><form name=f1>
<input type="button" name="b1" value="Numbers" onClick ="nos()">
</form>
</body>
</html>