0% found this document useful (0 votes)
14 views22 pages

JavaScript Programming Challenges

The document contains a series of JavaScript programs implemented in JSP format, each designed to perform various mathematical and logical operations. These include calculating the area of a triangle, finding simple interest, checking if a number is positive, negative, or zero, and determining prime numbers, among others. Each program prompts the user for input and displays the result on the webpage.

Uploaded by

sireeshakavi2009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

JavaScript Programming Challenges

The document contains a series of JavaScript programs implemented in JSP format, each designed to perform various mathematical and logical operations. These include calculating the area of a triangle, finding simple interest, checking if a number is positive, negative, or zero, and determining prime numbers, among others. Each program prompts the user for input and displays the result on the webpage.

Uploaded by

sireeshakavi2009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Javascript Problems

JSP – Java Script Program

<![Link] to calculate Area of Triangle>


<html>
<body>
<script language="javascript" type="text/javascript">
var b,h
b=Number(prompt("Enter base of triangle"))
h=Number(prompt("Enter height of triangle"))
{
a=((1/2)*(b)*(h))
}
[Link]("Area of triangle is "+a+" [Link]")
</script>
</body>
</html>

<![Link] to find Simple Interest>


<html>
<body>
<script language="javascript" type="text/javascript">
var P,T,R
P=Number(prompt("Enter principal amount"))
T=Number(prompt("Enter time(years)"))
R=Number(prompt("Enter rate"))
{
I=((P*T*R)/(100))
}
[Link]("Simple interest is "+I+" Rs"+"<br>")
</script>
</body>
</html>

<![Link] to check given number is Postive,Negative,or Zero>


<html>
<body>
<script language="javascript" type="text/javascript">
var n
n=Number(prompt("Enter an integer"))
if(n>0)
{
[Link](n,"is a positive number")
}
if(n<0)
{
[Link](n,"is a negative number")
}
if(n==0)
{
[Link]("The number is a zero")
}
</script>
</body>
</html>

<![Link] to find sum of n natural numbers>


<html>
<body>
<script language="javascript" type="text/javascript">
var i,n,s=0
n=Number(prompt("Enter a number"))
for(i=1;i<=n;i++)
{
s=s+i
}
[Link]("Sum of ",n," natural numbers is ",s)
</script>
</body>
</html>

<!Using while loop:>


<html>
<body>
<script language="javascript" type="text/javascript">
var i=1,n,s=0
n=Number(prompt("Enter number"))
while(i<=n)
{
s=s+i
i++
}
[Link]("Sum of "+n+" natural numbers is "+s)
</script>
</body>
</html>

<![Link] to check given number is a prime number or not>


<html>
<body>
<script language="javascript" type="text/javascript">
var i,n,c=0
n=Number(prompt("Enter a number"))
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c=c+1
}
}
if(c==2)
{
[Link](n," is a prime number")
}
else
{
[Link](n," is not a prime number")
}
</script>
</body>
</html>

<![Link] to find factors of a given number>


<html>
<body>
<script language="javascript" type="text/javascript">
var i,n
n=Number(prompt("Enter number"))
for(i=1;i<=n;i++)
{
if(n%i==0)
{
[Link](i,"<br>")
}
}
</script>
</body>
</html>

<![Link] to swap two variable values-Method 1:>


<html>
<body>
<script language="javascript" type="text/javascript">
var a,b
a=Number(prompt("Enter number a="))
b=Number(prompt("Enter number b="))
c=a
a=b
b=c
[Link]("a is "+a+"<br>")
[Link]("b is "+b+"<br>")
</script>
</body>
</html>

<!JSP to swap two variable values-Method 2:>


<html>
<body>
<script language="javascript" type="text/javascript">
var a,b
a=Number(prompt("Enter number"))
b=Number(prompt("Enter number"))
a=a+b
b=a-b
a=a-b
[Link]("a is "+a+"<br>")
[Link]("b is "+b+"<br>")
</script>
</body>
</html>
<![Link] to check given year is leap year or not>
<html>
<body>
<script language="javascript" type="text/javascript">
var year
year=Number(prompt("Enter a year:"))
if(year%100==0)
{
if(year%400==0)
{
[Link](year," is a leap year")
}
else
{
[Link](year," is not a leap year")
}
}
else if(year%4==0)
{
[Link](year," is a leap year")
}
else
{
[Link](year," is not a leap year")
}
</script>
</body>
</html>

<![Link] to find greatest number among 3 numbers>


<html>
<body>
<script language="javascript" type="text/javascript">
var a,b,c
a=Number(prompt("Enter number"))
b=Number(prompt("Enter number"))
c=Number(prompt("Enter number"))
if(a>b)
{
if(a>c)
{
[Link](a," is a greatest number")
}
else
{
[Link](c," is a greatest number")
}
}
else
{
if(b>c)
{
[Link](b," is a greatest number")
}
else
{
[Link](c," is a greatest number")
}
}
</script>
</body>
</html>

<![Link] to check given number is divisible by 4 and 8 or not>


<html>
<body>
<script language="javascript" type="text/javascript">
var n
n=Number(prompt("Enter number"))
if(n%4==0 && n%8==0)
{
[Link](n," is divisible by 4 and 8")
}
else
{
[Link](n," is not divisible by 4 and 8")
}
</script>
</body>
</html>
<![Link] to find the factorial of a given number>
<html>
<body>
<script language="javascript" type="text/javascript">
var i=1,n,f=1
n=Number(prompt("Enter number"))
while(i<=n)
{
f=f*i
i++
}
[Link]("Factorial of ",n," is",f)
</script>
</body>
</html>

<![Link] to print even numbers from 1 to n>


<html>
<body>
<script language="javascript" type="text/javascript">
var i=1,n
n=Number(prompt("Enter ur number"))
while(i<=n)
{
if(i%2==0)
{
[Link](i+"<br>")
}
i++
}
</script>
</body>
</html>

<![Link] to print odd numbers from 1 to n>


<html>
<body>
<script language="javascript" type="text/javascript">
var i=1,n
n=Number(prompt("Enter ur number"))
while(i<=n)
{
if(i%2==1)
{
[Link](i+"<br>")
}
i++
}
</script>
</body>
</html>

<![Link] to find the roots of a quadratic equation>


<html>
<body>
<script language="javascript" type="text/javascript">
var a,b,c
a=Number(prompt("Enter coefficient of x^2"))
b=Number(prompt("Enter coefficient of x"))
c=Number(prompt("Enter constant value"))
{
i1=((-b)+((b**2)-(4*a*c))**(0.5))/(2*a)
}
{
i2=((-b)-((b**2)-(4*a*c))**(0.5))/(2*a)
}
[Link]("First root is "+i1+"<br>")
[Link]("Second root is "+i2+"<br>")
</script>
</body>
</html>

<![Link] Program to print sum of values from 1 to 100 that are divisible by
7>
<html>
<body>
<script language="javascript" type="text/javascript">
var i=1,n=100,s=0
while(i<=n)
{
if(i%7==0)
{
s=s+i
}
i++
}
[Link]("Sum of values from 1 to 100 which are divisible by 7 is
",s,"<br>")
</script>
</body>
</html>

<![Link] to print prime numbers between 1 and 100>


<html>
<body>
<script language="javascript" type="text/javascript">
var i,j,n=100,c
for(i=1;i<=n;i++)
{
c=0
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c=c+1
}
}
if(c==2)
{
[Link](i,"<br>")
}
}
</script>
</body>
</html>

<![Link] to find total [Link] prime numbers below n>


<html>
<body>
<script language="javascript" type="text/javascript">
var i,j,n,c,f=0
n=Number(prompt("Enter number"))
for(i=1;i<=n;i++)
{
c=0
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c=c+1
}
}
if(c==2)
{
[Link](i,"<br>")
f=f+1
}
}
[Link]("Total [Link] prime numbers below ",n," is ",f,"<br>")
</script>
</body>
</html>

<![Link] to print no’s b/w 1 to 100 that are divisible by 11>


<html>
<body>
<script language="javascript" type="text/javascript">
var i=1,n=100
while(i<n)
{
if(i%11==0)
{
[Link](i,"<br>")
}
i=i+1
}
</script>
</body>
</html>

<![Link] to check the given number is palindrome or not>


<html>
<body>
<script language="javascript" type="text/javascript">
var i,n,r,rev=0
n=Number(prompt("Enter number"))
i=n
while(i!=0)
{
r=i%10
rev=rev*10+r
i=[Link](i/10)
}
if(rev==n)
{
[Link](n," is a palindrome number")
}
else
{
[Link](n," is not a palindrome number")
}
</script>
</body>
</html>

<![Link] to print the table for the given n value upto 10 times>
<html>
<body>
<script language="javascript" type="text/javascript">
var n,i=1
n=Number(prompt("Enter number"))
while(i<=10)
{
[Link](n,"*",i,"=",n*i,"<br>")
i=i+1
}
</script>
</body>
</html>

<![Link] to find greatest common divisor(GCD/HCF)>


<html>
<body>
<script language="javascript" type="text/javascript">
var a,b,r
a1=Number(prompt("Enter number"))
b1=Number(prompt("Enter number"))
a=a1
b=b1
r=a%b
while(r!=0)
{
a=b
b=r
r=a%b
}
[Link]("GCD/HCF of ",a1,"&",b1," is ",b)
</script>
</body>
</html>
<![Link] to print whether given num n is perfect number/not>
<html>
<body>
<script language="javascript" type="text/javascript">
var n,i,s=0
n=Number(prompt("Enter number"))
for(i=1;i<n;i++)
{
if(n%i==0)
{
s=s+i
}
}
if(s==n)
{
[Link](n," is a perfect number")
}
else
{
[Link](n," is not a perfect number")
}
</script>
</body>
</html>

<![Link] to print perfect numbers between 1 to n>


<html>
<body>
<script language="javascript" type="text/javascript">
var n,i
n=Number(prompt("Enter number"))
for(i=1;i<=n;i++)
{
s=0
for(j=1;j<i;j++)
{
if(i%j==0)
{
s=s+j
}
}
if(s==i)
{
[Link](i,"<br>")
}
}
</script>
</body>
</html>
<![Link] to check given num is perfect square / perfect cube>
<html>
<body>
<script language="javascript" type="text/javascript">
var i=1,n,c=0
n=Number(prompt("Enter number"))
while(i<n)
{
if(n==i*i)
{
c=1
}
if(n==i*i*i)
{
c=2
}
i=i+1
}
if(c==1)
{
[Link](n," is a perfect square")
}
else
{
if(c==2)
{
[Link](n," is a perfect cube")
}
else
{
[Link](n," is not a perfect square & not a perfect cube")
}
}
</script>
</body>
</html>

<![Link] to print fibonacci series upto 'n'th term>


<html>
<body>
<script language="javascript" type="text/javascript">
var n,a=0,b=1,c,count=0
n=Number(prompt("Enter number"))
[Link]("Fibonacci series upto ",n,"th term","<br>")
while(count<n)
{
if(n==1)
{
[Link](a,"<br>")
break;
}
else if(n==2)
{
[Link](a,"<br>")
[Link](b,"<br>")
break;
}
else
{
[Link](a,"<br>")
c=a+b
a=b
b=c
count++
}
}
</script>
</body>
</html>

You might also like