0% found this document useful (0 votes)
57 views

JavaScript Programs

It's a java script program for breginner to learn the basics

Uploaded by

Study With Krish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

JavaScript Programs

It's a java script program for breginner to learn the basics

Uploaded by

Study With Krish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Sample JavaScript Programs

1. Write event driven JavaScript program to display double of accepted number.


<html>
<head>
<script language="JavaScript">
function doub()
{
var a;
a=parseInt(document.f1.t1.value);
document.write("Double = ", a*2);
}
</script>
</head>
<body>
<form name=f1>
Enter any number:<input type=text name=t1><br>
<input type="button" name="b1" value="Double" onClick =" doub()">
</form>
</body>
</html>

2. Write event driven program in JavaScript to find area of triangle.


(Area =1/2 *base * height).
<html>
<head>
<script language="JavaScript">
function area()
{
var b1,h1;
b1=parseFloat(document.f1.b.value);
h1=parseFloat(document.f1.h.value);
document.write("Area of Triangle = ",1/2 * b1 * h1);
}
</script>
</head>
<body>
<form name=f1>
Enter Base :<input type=text name=b><br>
Enter Height :<input type=text name=h><p>
<input type="button" name="b1" value="Find Area" onClick ="area()">
</form>
</body>
</html>

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>

4. Write event driven program in JavaScript to find perimeter of rectangle.


(Perimeter =2 *base+2 * height).
<html>
<head>
<script language="JavaScript">
function peri()
{
var b1,h1;
b1=parseFloat(document.f1.b.value);
h1=parseFloat(document.f1.h.value);
document.write("Perimeter of Rectangle = ", 2 * b1 + 2 * h1);
}
</script>
</head>
<body>
<form name=f1>
Enter Base :<input type=text name=b><br>
Enter Height :<input type=text name=h><p>
<input type="button" name="b1" value="Perimeter" onClick ="peri()">
</form>
</body>
</html>

5. Write event driven program in JavaScript to find perimeter of a trapezium.


(Perimeter =a+b+c+d).
<html>
<head>
<script language="JavaScript">
function peri()
{
var a,b,c,d;
a=parseFloat(document.f1.p1.value);
b=parseFloat(document.f1.p2.value);
c=parseFloat(document.f1.n1.value);
d=parseFloat(document.f1.n2.value);
document.write("Perimeter of Trapezium = ",a+b+c+d);
}
</script>
</head>
<body>
<form name=f1>
Length of 1st parallel side:<input type=text name=p1><br>
Length of 2nd parallel side:<input type=text name=p2><br>
Length of 1st non parallel side:<input type=text name=n1><br>
Length of 2nd non parallel side:<input type=text name=n2><p>
<input type="button" name="b1" value="Perimeter" onClick ="peri()">
</form>
</body>
</html>

6. Write Event Driven program in JavaScript for subtraction of two accepted


numbers.
<html>
<head>
<script language="JavaScript">
function sub()
{
var a,b;
a=parseInt(document.f1.t1.value);
b=parseInt(document.f1.t2.value);
document.write("Answer = ",a -b);
}
</script>
</head>
<body>
<form name=f1>
Enter 1st no: <input name=t1><br>
Enter 2nd no: <input name=t2><br>
<input type="button" name="b1" value="Subtract" onClick ="sub()">
</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>

10.Write event driven program in JavaScript to display the series as 10, 9, 8.


<html>
<head>
<script language="JavaScript">
function num()
{
var i;
for(i=10;i>=1;i--)
{
document.write(i+" ");
}
}
</script></head>
<body><form name=f1>
<input type="button" name="b1" value="Show Series" onClick ="num()">
</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>

12.Write event driven program in JavaScript to display the series as 1, 3, 5,…..11.


<html>
<head>
<script language="JavaScript">
function odd()
{
var sum,i;
for(i=1;i<=11;i+=2)
{ document.write(i+" "); }
}
</script>
</head>
<body>
<form name=f1>
<input type="button" name="b1" value="Number Series" onClick=odd()">
</form>
</body>
</html>

13.Write program in JavaScript to find factorial of 5 on click event.


<html>
<head>
<script language="JavaScript">
function fact()
{
var f;
f=1;
for(i=1;i<=5;i++)
{
f=f*i;
}
document.write("Factorial of 5 is = "+f);
}
</script>
</head>
<body>
<form name=f1>
<input type="button" name="b1" value="Factorial" onClick ="fact()">
</form>
</body>
</html>

14.Write event driven JavaScript program to find factorial of accepted number.


(For ex. Factorial of 5 = 5 * 4 * 3 * 2 * 1 = 120)
<html>
<head>
<script language="JavaScript">
function fact()
{
var a,f;
f=1;
a=document.f1.t1.value;
for(i=1;i<=a;i++)
{
f=f*i;
}
document.write("Factorial="+f);
}
</script>
</head>
<body>
<form name=f1>
Enter any no: <input type=text name=t1><br>
<input type="button" name="b1" value="Factorial" onClick ="fact()">
</form>
</body>
</html>

You might also like