Write a the sum JavaScript to calculate of first 10 natural nos.
<html>
<head>
<title> Javascript program</title>
</head>
<body>
<script language= "JavaScript">
var i,t;
t=0;
document.write("First ten natural numbers are:");
for(i=1;i<=10;i++)
{
t=t+i;
document.write("<br>"+i);
}
document.write("<br>");
document.write("Sum="+t);
</script>
</body></html>
Write a JavaScript program to add all numbers in between any two given numbers.
<HTML>
<BODY>
<SCRIPT>
var a,b,i,s;
s=0;
a=parseInt(prompt("Enter 1st no."));
b=parseInt(prompt("Enter 2nd no."));
for(i=a;i<=b;i++)
{
s=s+i;
}
document.write("Total=",s);
</SCRIPT>
</BODY></HTML>
Write JavaScript program to display table of 5.
<html>
<body>
<script>
var i,t;
t=0;
document.write("Table of 5:");
for(i=1;i<=10;i++)
{
t=i*5;
document.write("<br>"+t);
}
</script>
</body>
</html>
Write JavaScript program to display table of any given number.
<html>
<body>
<script>
var i,a,t;
t=0;
a=parseInt(prompt("Enter any number"));
document.write("Table of ",a, ":");
for(i=1;i<=10;i++)
{
t=i*a;
document.write("<br>"+t);
}
</script></body></html>
Write JavaScript program to calculate the reverse of the given number.
<html>
<body>
<script language ="JavaScript">
var n,r,s;
s=0;
n=prompt("Enter any number");
n1=n;
while(n!= 0)
{
r = n % 10;
s = s * 10 + r;
n = parseInt(n/10);
}
document.write("The reverse of " + n1 + " is " + s);
</script>
</body> </html>
Write a JavaScript code to display even numbers between 0 to 10.
<HTML>
<HEAD>
<TITLE> Even Number </TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
document.write("The even numbers between 0 to 10 are as follows");
for(i=0;i<=10;i++)
{
if(i % 2 == 0)
{
document.write("<br>"+i);
}
}
</SCRIPT></BODY></HTML>
Write a JavaScript code to display all odd numbers between any two given numbers.
<HTML>
<HEAD><TITLE> odd numbers </TITLE></HEAD>
<BODY>
<SCRIPT>
var a,b,i;
a=parseInt(prompt("Enter 1st no."));
b=parseInt(prompt("Enter 2nd no."));
document.write("Odd nos betn ", a , " and ",b, " are : <br>");
for(i=a;i<=b;i++)
{
if(i % 2 == 1)
{
document.write("<br>"+i);
}
}
</SCRIPT>
</BODY>
</HTML>
Write a JavaScript code to display all numbers in between 1 and 30 which are divisible by 3.
<HTML>
<BODY>
<SCRIPT>
var i;
for(i=1;i<=30;i++)
{
if(i % 3 == 0)
{
document.write("<br>"+i);
}
}
</SCRIPT>
</BODY>
</HTML>
Write a JavaScript code to display all numbers in between any two given numbers which are not
divisible by 3.
<HTML>
<BODY>
<SCRIPT>
var a,b,i;
a=parseInt(prompt("Enter 1st no."));
b=parseInt(prompt("Enter 2nd no."));
for(i=a;i<=b;i++)
{
if(i % 3 != 0)
{
document.write("<br>"+i);
}
}
</SCRIPT></BODY></HTML>
Write a JavaScript code to calculate double of the given number. (Event based)
<html>
<head>
<script language="Javascript">
/*To calculate the double of any given no. */
function test()
{
document.write("Double: ", n1.value * 2);
}
</script></head><body>
<input type="text" name="n1" value=0> <br>
<input type="button" name="a" value="Calculate" onClick ="test()">
</body></html>
Write a JavaScript code to accept a day and checks whether is a weekend or a working day.
<html>
<head>
<title> JavaScript Program </title>
</head>
<body>
<script language = "JavaScript">
day = prompt("Enter day:");
if(day=="sunday")
{
document.writeln("It is weekend");
}
else
{
document.writeln("It is working day");
}
</script>
</body>
</html>
Write a JavaScript code to calculate Fibonacci Series up to N terms. Where N is any given
number.
<html>
<head><title> Fibonacci Series </title>
</head>
<body>
<script language = "JavaScript">
var a=1;
var b=1;
var n2=prompt("Enter last number");
document.write("Fibonacci Series:");
document.write("<br>"+a);
document.write("<br>"+b);
for(i=3;i<=n2;i++)
{
var c=a+b;
document.write("<br>"+c);
a=b;
b=c;
}
</script>
</body>
</html>
Write a JavaScript code to calculate square and cube of the number.
<html>
<head>
</head>
<body>
<script language="Javascript">
/*To calculate the square and cube of any given no. */
var a;
a=prompt("Enter any number");
b=parseInt(a);
document.write("Square: ", b * b);
document.write("<br>");
document.write("Cube: ", b * b * b);
</script>
</body>
</html>
Write a JavaScript code using mouse event so that by moving the mouse pointer over
the text, the image should display.
<html>
<head>
<title> JavaScript Program </title>
<script language = "JavaScript">
function iload()
{
document.writeln('<img src="table.bmp">');
document.writeln("Welcome");
}
</script>
</head>
<body>
<a onMouseover="iload();">Welcome</a>
</body>
</html>
Write a JavaScript program to find greatest no. from any three given nos.
<html>
<head>
<script language ="Javascript">
//JavaScript to find greatest no.
function test()
{
a=n1.value;
b=n2.value;
c=n3.value;
document.write(“Greatest no. 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>
Enter any three nos:
<br>
<input type="text" name="n1"> <br>
<input type="text" name="n2"> <br>
<input type="text" name="n3"> <br>
<input type="button" name="button1" value="Check" onClick = "test()">
</body>
</html>
Write a JavaScript to calculate the sum of first N natural nos., where N is a any given
no.
<html>
<head></head><body>
<script language= "JavaScript">
var i,t,n;
i=1;
t=0;
n=prompt("Enter any number");
for(i=1;i<=n;i++)
{
t=t+i;
document.write('<br>'+t);
}
document.write('<br>');
document.write("Total="+t);
}
</script>
</body>
</html>
Write a JavaScript to change background color of the document by using mouseover
event of button.
<html>
<head>
<title> JavaScript Program </title>
<SCRIPT LANGUAGE = "JavaScript">
//Use of Mouseover and Mouseout event.
function test(col)
{
document.bgColor=col;
}
</SCRIPT>
</head>
<body>
<input type="button" name="n1" value="Red" onMouseover="test('red')"
onMouseout="test('black')">
<input type="button" name="n2" value="Blue" onMouseover="test('blue')"
onMouseout="test('green')">
</body>
</html>
Write a JavaScript to check whether the given text is number or not.
<html>
<head>
<body>
<script language ="javascript">
//Use of isNaN() function
var a,t;
t=0;
a=prompt("Enter any character");
t=isNaN(a);
if(t==false)
{
document.write("Character is number");
}
else
{
document.write("Character is not number");
}
</script>
</body>
</html>
Write a JavaScript to check whether the given no. is +ve or –ve or zero. (Mar-2004)
<html>
<head>
</head>
<body>
<script language ="Javascript">
//To check where the given no. is +ve or -ve or zero
var n;
n=prompt("Enter any number");
if(n>0)
{
document.write("+ve no.");
}
else
{
if(n<0)
{
document.write("-ve no.");
}
else
{
document.write("zero no.");
}
}
</script></body></html>
Write a JavaScript to display the following output.
*
*
*
*
*
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
document.write('*');
document.write('<br>');
}
</script>
</body>
</html>
Write a JavaScript to display the following output.
*****
*****
*****
*****
*****
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
document.write('*');
}
document.write('<br>');
}
</script>
</body>
</html>
Write a JavaScript to display the following output.
*
**
***
****
*****
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
document.write('*');
}
document.write('<br>');
}
</script>
</body>
</html>
Write a JavaScript to display the following output.
1
12
123
1234
12345
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
document.write(j);
}
document.write('<br>'); }</script></body>
Write a JavaScript to display the following output.
1
22
333
4444
55555
</html>
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
document.write(i);
}
document.write('<br>');
}
</script>
</body>
</html>
Write a JavaScript script to accept the first, middle and last names of the user and print the
name.
<html>
<head>
<script language ="Javascript">
//JavaScript to find greatest no.
function test()
{
a=n1.value;
b=n2.value;
c=n3.value;
document.write("Name is: ",a +" "+ b+" "+c);
}
</script></head><body>
F_Name: <input type="text" name="n1"> <br>
M_Name: <input type="text" name="n2"> <br>
L_Name: <input type="text" name="n3"> <br>
<input type="button" name="button1" value="Join" onClick = "test()">
</body>
</html>
Write a JavaScript to display the following output.
*****
****
***
**
*
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
document.write('*');
}
document.write('<br>');
}
</script>
</body>
</html>
Write a JavaScript to display the following output.
12345
1234
123
12
1
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
document.write(j);
}
document.write('<br>');
}
</script></body></html>
Write a JavaScript to display the following output.
*
**
***
****
*****
****
***
**
*
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
document.write('*');
}
document.write('<br>');
}
for(i=2;i<=5;i++)
{
for(j=5;j>=i;j--)
{
document.write('*');
}
document.write('<br>');
}
</script>
</body>
</html>
Write a JavaScript to display the following output.
*****
****
***
**
*
**
***
****
*****
<html>
<head>
<title> Output </title>
</head>
<body>
<script language ="javascript">
var i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
document.write('*');
}
document.write('<br>');
}
for(i=2;i<=5;i++)
{
for(j=1;j<=i;j++)
{
document.write('*');
}
document.write('<br>');
}
</script>
</body>
</html>