1) Accept the number from the user and Write a code using JavaScript to print the multiplication table of the entered number. Display the table in the tabular format.
Solution:-
<html>
<head><title> for loop</title>
</head>
<script Language="JavaScript">
var num=prompt("Please enter the number");
for(i=1;i<=10;i++)
{
document.write(num+"X"+i+"="+num*i)
document.write("<br>");
}
</script>
</body>
</html>
2) Write a program in JavaScript to display the reverse of the number. For eg. Reverse of 678 is 876
Solutuion:-
<html><head><title>sum digits </title></head>
<body>
<script language="JavaScript">
var q=1;
var rem;
var n=parseInt(prompt("Please enter the number"));
document.write("The number is :",n);
document.write("<br>");
while(q>0)
{
rem=n%10;
document.write(rem);
q=parseInt(n/10);
n=q;
}
</script>
</body>
</html>
3) Write a program in JavaScript to accept a number and find whether the accepted number is Prime number or not
Solution:-
<html>
<head><title> do while loop</title>
</head>
<script Language="JavaScript">
var num,i;
num=prompt("please enter the number");
flag=0;
for(i=2;i<num-1;i++)
{
var p=num%i;
if(p==0)
{
flag=1;
}
}
if(flag==1)
{
document.write("Entered number is not a prime number");
}
else
{
document.write("Entered number is a prime number");
}
</script>
</body></html>
4) Write a program in JavaScript to display all the even numbers from 120 to 150
Solution:-
<html>
<head><title> do while loop</title>
</head>
<script Language="JavaScript">
var num;
for(num=120;num<=150;num=num+2)
{
document.write(num);
document.write("<br>");
}
</script>
</body>
</html>
5) Write a program in JavaScript to print factorial of a number entered by the user.
Solution:-
<html><head><title>factorial </title></head>
<body>
<script language="JavaScript">
var no,i;
var fact=1;
no=prompt("Please enter the number :");
for(i=1;i<=no;i++)
{
fact=fact*i;
}
document.write("The factorial=",fact);
</script>
</body>
</html>
6)
Write a program in JavaScript to print Fibonacci series:( It is the series of numbers such that the sum of the previous two numbers is equal to the first number For example : 1 ,2,3,5,8,13,21,34……….)
Solution:-
<html><head><title>Fibonacci </title></head>
<body>
<script language="JavaScript">
//Program to display Fibonacci series
var a=1;
var b=2;
var c=0;
var i,n;
n=parseInt(prompt("Please enter the value of n"));
document.write("The series is :");
document.write("<br>");
document.write(a+"\t"+b+"\t");
for(i=0;i<=n;i++)
{
c=a+b;
a=b;
b=c;
document.write(c+"\t");
}
</script>
<br>
© All rights reserved
</body>
</html>
7) Write a program in JavaScript to display the following pattern.
*
**
***
**
***
****
*****
*****
Solution:-
<html><head><title>sum digits </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>
8) Write a program in JavaScript to display the following pattern.
12
123
1234
12345
Solution:-
<html><head><title>star patt </title></head>
<body>
<script language="JavaScript">
/* Program to display Star patteren
on the web page*/
var i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i; j++)
{
document.write(j);
}
document.write("<br>");
}
//here the script ends
</script>
</body>
</html>
9) Write a program in JavaScript to display the following pattern.
4321
432
43
4
Solution:-
<html><head><title>star patt </title></head>
<body>
<script language="JavaScript">
/* Program to display pattern
on the web page*/
var i,j;
for(i=1;i<=4;i++)
{
for(j=4;j>=i; j--)
{
document.write(j);
}
document.write("<br>");
}
//here the script ends
</script>
</body>
</html>
10) Write a program in JavaScript to display the following pattern.
*****
****
***
**
*
****
***
**
*
Solution;-
<html><head><title>sum digits </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>
11) Write a program in JavaScript to display the following pattern.
1234
123
12
1
12
123
1234
Solution:-
<html><head><title>pattern </title></head>
<body>
<script language="JavaScript">
/* Program to display pattern
on the web page*/
var i,j;
for(i=4;i>=1;i--)
{
for(j=1;j<=i; j++)
{
document.write(j);
}
document.write("<br>");
}
for(i=2;i<=4;i++)
{
for(j=1;j<=i; j++)
{
document.write(j);
}
document.write("<br>");
}
//here the script ends
</script>
</body>
</html>
No comments:
Post a Comment