Javascript
1. Write a JavaScript program to add two numbers
<html>
<head>
<title>Sum of Two Numbers</title>
</head>
<body>
<script>
var a = parseInt(prompt("Enter 1st number"));
var b = parseInt(prompt("Enter 2nd number"));
var sum = a + b;
document.write("The sum is: " + sum);
</script>
</body>
</html>
Using function
<html>
<head>
<title>Sum of Two Numbers</title>
</head>
<body>
<script>
var a = parseInt(prompt("Enter 1st number"));
var b = parseInt(prompt("Enter 2nd number"));
function sum(a, b) {
var x = a + b;
document.write("The sum is: " + x);
}
sum(a, b);
</script>
</body>
</html>
2.Write a JavaScript program to find simple interest.
<html>
<head>
<title>Simple interest</title>
</head>
<body>
<script>
var p = parseInt(prompt("Enter Principle"));
var t = parseInt(prompt("Enter time"));
var r = parseInt(prompt("Enter rate"));
var si = (p*t*r)/100;
document.write("Simple Interest=Rs " +si);
</script>
</body>
</html>
Using Function
<html>
<head>
<title>Area of circle</title>
</head>
<body>
<script>
var p = parseInt(prompt("Enter principle"));
var t = parseInt(prompt("Enter time"));
var r = parseInt(prompt("Enter rate"));
function si(p,t,r)
{
var si = (p*t*r)/100;
document.write("Simple interest=Rs" +si);
}
si(p,t,r);
</script>
</body>
</html>
3. Write a JavaScript program to find area of circle.
<html>
<head>
<title>area of circle</title>
</head>
<body>
<script>
var r = parseInt(prompt("Enter radius"));
var area = (22/7)*r*r;
document.write("Area of circle=" +area);
</script>
</body>
</html>
Using function
<html>
<head>
<title>Area of circle</title>
</head>
<body>
<script>
var r = parseInt(prompt("Enter radius"));
function area(r)
{
var area = (22/7)*(r*r);
document.write("Area of circle: " + area);
}
area(r);
</script>
</body>
</html>
4.Write a JavaScript program to find greatest among of two numbers.
<html>
<head>
<title>Greatest among of two numbers</title>
</head>
<body>
<script>
var a = parseInt(prompt("Enter 1st number"));
var b = parseInt(prompt("Enter 2nd number"));
if(a>b)
{
alert(a+ "is greatest");
}
else
{
alert(b+ "is greatest");
}
</script>
</body>
</html>
Using Function
<html>
<head>
<title>Greatest among of two numbers</title>
</head>
<body>
<script>
var a = parseInt(prompt("Enter 1st number"));
var b = parseInt(prompt("Enter 2nd number"));
function greatest(a,b)
{
if(a>b)
{
alert(a+ "is greatest");
}
else
{
alert(b+ "is greatest");
}
}
greatest(a,b);
</script>
</body>
</html>
5. Write a JavaScript program to find greatest among of three numbers.
<html>
<head>
<title> Greatest among of three numbers</title>
</head>
<body>
<script>
var a = parseInt(prompt("Enter 1st number"));
var b = parseInt(prompt("Enter 2nd number"));
var c = parseInt(prompt("Enter 3rd number"));
if(a>b&&a>c)
{
alert(a+ "is greatest");
}
else if(b>a&&b>c)
{
alert(b+ "is greatest");
}
else
{
alert(c+ "is greatest");
}
</script>
</body>
<html>
<head>
<title> series</title>
</head>
<body>
<script>
for(i=1;i<=10;i=i+1)
{
document.write(i +"<br>");
}
</script>
</body>
</html>
9. 1,3,5,……… upto 10th term
<html>
<head>
<title> series</title>
</head>
<body>
<script>
a=1;
for(i=1;i<=10;i=i+1)
{
document.write(a +" ");
a=a+2;
}
</script>
</body>
</html>
10.2,4,6…..upto 10th term
<html>
<head>
<title> series</title>
</head>
<body>
<script>
a=2;
for(i=1;i<=10;i=i+1)
{
document.write(a +" ");
a=a+2;
}
</script>
</body>
</html>
]12. 2 4 7 11 16 ……….upto 10th term
<html>
<head>
<title> series</title>
</head>
<body>
<script>
a=1;
for(i=1;i<=10;i=i+1)
{
document.write(a +" ");
a=a+i;
}
</script>
</body>
</html>
13. 5 10 15 20……………..upto 10th term
<html>
<head>
<title> series</title>
</head>
<body>
<script>
for(i=1;i<=10;i=i+1)
{
a=5;
a=a*i;
document.write(a +" ");
}
</script>
</body>
14. 5,25 125 625………..upto 10th term
<html>
<head>
<title> series</title>
</head>
<body>
<script>
a=5;
for(i=1;i<=10;i=i+1)
{
document.write(a +" ");
a=a*5;
}
</script>
</body>
</html>
15. Write a JavaScript Code snippet, along with HTML, that displays the multiplication table of a entered number.
<html>
<head>
<title>Multiplication Table</title>
</head>
<body>
<script>
var a = parseInt(prompt("Enter a number"));
for (var i = 1; i <= 10; i++) {
document.write(a + " × " + i + " = " + (a * i) + "<br>");
}
</script>
</body>
</html>1
6. Write a JavaScript code to calculate the factorial of a given number.
<html>
<head>
<title>Factorial of a Number</title>
</head>
<body>
<script>
var n = parseInt(prompt("Enter a number"));
var fact = 1;
for (var i = 1; i <= n; i++) {
fact = fact * i;
}
document.write("Factorial of " + n + " is: " + fact);
</script>
</body>
</html>
Write a program to display factors of a given number.
<html>
<head>
<title>Factor</title>
</head>
<body>
<script>
var n = parseInt(prompt("Enter a number"));
document.write("Factors: ");
for (var i = 1; i <= n; i++) {
if (n % i === 0) {
document.write(i + " ");
}
}
</script>
</body>
</html>
onclick Event Type
This is the most frequently used event type which occurs when a user clicks the left button of his mouse. User can put validation, warning etc., against this event type.
Example:
<html> <head> <title>on click</title> </head> <script type="text/javascript"> <!-- function sayHello() { alert("Hello World") } //--> </script> <body> <p>Click the following button and see result</p> <form> <input type="button" onclick= "sayHello()" value="Say Hello"/> </form> </body> </html>