Tuesday, October 21, 2014

Some Important Small Java Script Programs


1. Write a Java Script program that show message “My First JavaScript program”

<html>
<body>
<script>

    document.writeln ( "My First JavaScript program")

</script>
</body>
</html>
……………………………………………………………………….

2. Write a Java Script program that implements fibonacci series.
<html>
<body>
<script type="text/javascript">
          var a=0,b=1,c;
          document.write("Fibonacci Number becomes=<br/>");
          document.write(a);
          document.write("<br/>");
          document.write(b);
          while (b<=10)
          {
                  
                   c=a+b;                
                   document.write("<br/>");
                   document.write(c);
                   a=b;
                   b=c;
                  
          }
</script>
</body>
</html>
………………………………………………………………………..

3. Write a Java Script program that check odd or even number
<html>
<head>
<script type="text/javascript">
var n = prompt("Enter a number ");
n = parseInt(n);
if (isNaN(n))
{
alert("Please Enter a Number");
}
else if (n == 0)
{
alert("The number is zero");
}
else if (n%2)
{
alert("The number is odd");
}
else
{
alert("The number is even");
}
</script>
</head>
<body>
</body>
</html>
…………………………………………………………………………

4. Write a Java Script program that switch between two web sites.

<html>
<head>
<script type="text/javaScript">
function see()
{
var c= confirm("Click OK to see Google Homepage or CANCEL to see wbut Homepage");
if (c== true)
{
window.location="http://www.google.co.in/";
}
else
{
window.location="http://www.wbutech.com/";
}
}
</script>
</head>
<body>
<center>
<form>
<input type="button" value="Click to chose either Google or wbut" onclick="see()">
</form>
</center>
</body>
</html>
……………………………………………………………………………….

5. Write a Java Script program that check string is palindrome or not.

<html>
<body>
<script type="text/javascript">
  function Palindrome() {
   var revStr = "";
   var str = document.getElementById("str").value;
   var i = str.length;
   for(var j=i; j>=0; j--) {
      revStr = revStr+str.charAt(j);
   }
   if(str == revStr) {
      alert(str+" -is Palindrome");
   } else {
      alert(str+" -is not a Palindrome");
   }
}
</script>
<form >
  Enter a String/Number: <input type="text" id="str" name="string" /><br />
  <input type="submit" value="Check" onclick="Palindrome();"/>
</form>
</body>
</html>
………………………………………………………………………………

6. Write a Java Script program that shows different day (input chosen by user)
<html>
<head>
<script type="text/javascript">
var n=prompt("Enter a number between 1 and 7");
switch (n)
{
case (n="1"):
document.write("Sunday");
break;
case (n="2"):
document.write("Monday");
break;
case (n="3"):
document.write("Tuesday");
break;
case (n="4"):
document.write("Wednesday");
break;
case (n="5"):
document.write("Thursday");
break;
case (n="6"):
document.write("Friday");
break;
case (n="7"):
document.write("Saturday");
break;
default:
document.write("Invalid Weekday");
break
}
</script>
</head>
</html>
……………………………………………………………….

7. Write a Java Script program addition of two numbers.

<html>
<head>
<script type="text/javascript">
    var num1, num2, sum
    num1 = prompt("Enter first number")
    num2 = prompt("Enter second number")
    sum = parseInt(num1) + parseInt(num2) // "+" means "add"
    alert("Sum = " + sum)  // "+" means combine into a string
</script>
</head>
</html>
…………………………………………………………………….

8. Write a Java Script program find out the radius of the circle
<html>
<head>
<script type="text/javascript">
   var rad, area;
    rad = prompt("Enter radius")
    while (rad != null) {
          area = 3.14 * rad * rad
        alert("radius = " + rad + ", area = " + area)
        document.write("<br> radius = " + rad + ", area = " + area)
          rad = prompt("Enter radius")
    }
</script>
</head>
</html>
………………………………………………………………………

9. Write a Java Script program that shows the area of the circle.
<html>
<head>
<script type="text/javascript">
   var ln,br, area;
    ln = prompt("Enter height of the rectangle")
    br = prompt("Enter depth of the rectangle")
          area = ln*br
        alert("height = " + ln + ", depth ="+br+", area of the rectangle = " + area)
        document.write("height = " + ln + ", depth ="+br+", area area of the rectangle = " + area)
         
</script>
</head>
</html>
………………………………………………………………….

10. Write a Java Script program that merge two strings.
<html>
<head>
<script type="text/javascript">
   var firstname, secondname, result
    firstname = prompt("Enter first name")
    secondname = prompt("Enter last name")
    result = firstname + secondname  // + means "join" here
    alert("hello, " + result)
         
</script>
</head>
</html>
……………………………………………………………………..

11. Write a Java Script program that sorting strings using array.
<html>
<head>
<script type="text/javascript">
   var name, i = 0, j, temp
    var names = new Array()

    // fill the array with names
    name = prompt("Enter new name, or write ok to end")
    while (name != "ok") {
        document.write("<br> " + name)
          names[names.length] = name
          name = prompt("Enter new name, or write ok to end")
    }

    document.write("<P> " + names.length + " names")
   
    // insertion sort
    for (i = 0; i < names.length-1; i++) {
        for (j = i+1; j < names.length; j++) {
            if (names[i] > names[j]) {
                temp = names[i]
                names[i] = names[j]
                names[j] = temp
            }
        }
    }

    // print names
    for (i = 0; i < names.length; i++) {
        document.write("<br> " + names[i])
    }

         
</script>
</head>
</html>
…………………………………………………………………………

12. Write a Java Script program that calculate factorial of a number.
<html>
<head>
<script type="text/javascript">
function factorial(f,n)
{
l=1;
for(i=1;i<=n;i++)
l=l*i;
f.p.value=l;
}
</script>
</head>
<body>
<form>
number:<input type="text" name="t"></br>
<input type="button" value="submit" onClick="factorial(this.form,t.value)"></br>
result:<input type="text" name="p"></br>
</form>
</body>
</html>
………………………………………………………………..

13. Write a Java Script program that check Armstrong number.
<html>
<head>
<script language="JavaScript">
var b,z,c=0;
var a=prompt("Enter a number");
z=a;
while(z>0)
{
b=z%10;
c=c+(b*b*b);
z=parseInt(z/10);
}
if(a==c)
alert("given no is amstrong number");
else
alert("given no is not an amstrong number");
</script>
</head>

</html>
…………………………………………………………………….

14. Write Java Script program check a year is leap year or not.

<html>
<head>
<script language="JavaScript">
var b,c;
var a=prompt("Enter year");
b=a%4;
c=a%400;
if(b==0||c==0)
{
alert("given year is leap year");
}
else
alert("given year is not a leap year");
</script>
</head>

</html>
………………………………………………………………………

15. Write a Java Script program to print perfect number
<html>
<head>
<script language="JavaScript">
var a=prompt("enter a value");
alert(a);
var z=a;
var c=0;
for(var i=1;i<a;i++)
{
//var c=0;
if(a%i==0)
{
c=c+i;
//alert("Now C IS:"+c);
}
}
if(c==z)
{
alert("given no is perfect");
}
else
{
alert("given no is not a perfect");
}
</script>
</head>

</html>
…………………………………………………………………………..

16. Write a Java Script program that shows the number is prime or not.
<html>
<head>
<script language="JavaScript"> 
var a=prompt("enter a value"); 
alert(a); 
var c=0; 
 
 for(var i=2;i<a;i++) 
  { 
   if(a%i==0) 
   { 
    c=i; 
    //alert("Now C IS:"+c); 
   } 
  } 
  if(c==0) 
  { 
   alert("given no is prime"); 
  } 
  else 
  { 
   alert("given no is not a prime"); 
  } 
</script> 
</head>

</html>
…………………………………………………………………..

17. Write a Java Script program reverse of a number

<html>
<head>
 <script language="JavaScript">
var a=prompt("Enter a value");
var b,sum=0;
var z=a;
while(a>0)
{
b=a%10;
sum=sum*10+b;
a=parseInt(a/10);
}
alert(sum);
</script>
</head>

</html>

No comments:

Post a Comment