JavaScript Programs

 1. Add of two numbers

Method 1:

<!doctype html>
<html>
   <head>
      <title>Adding two numbers using JavaScript in HTML</title>
      <script>
         var num1 = 10;
         var num2 = 40;
         var sum = num1+num2;
         document.write("Sum of two numbers is " + sum);
      </script>
   </head>
   <body></body>
</html>


Method 2:

<!doctype html>
<html>
   <head>
      <script>
         function add()
         {
           var num1, num2, sum;
           num1 = parseInt(document.getElementById("firstnumber").value);
           num2 = parseInt(document.getElementById("secondnumber").value);
           sum = num1 + num2;
           document.getElementById("answer").value = sum;
         }
      </script>
   </head>
   <body>
      <p>First Number: <input id="firstnumber"></p>
      <p>Second Number: <input id="secondnumber"></p>
      <button onclick="add()">Add Them</button>
      <p>Sum = <input id="answer"></p>
   </body>
</html>


Method 3:


<!doctype html>
<html>
   <head>
      <script>
         var num1, num2, sum;
         function FirstNum()
         {
           num1 = parseInt(document.getElementById("firstNumber").value);
           if(num1)
           {
             temp = document.getElementById("blockOne");
             temp.style.display = "none";
             temp = document.getElementById("blockTwo");
             temp.style.display = "block";
           }
         }
         function SecondNum()
         {
           num2 = parseInt(document.getElementById("secondNumber").value);
           if(num1 && num2)
           {
             temp = document.getElementById("blockOne");
             temp.style.display = "none";
             temp = document.getElementById("blockTwo");
             temp.style.display = "none";
             sum = num1 + num2;
             temp = document.getElementById("blockThree");
             temp.style.display = "block";
             document.getElementById("res").innerHTML = sum;
           }
         }
      </script>
   </head>
   <body>
      <p id="blockOne"> First Number <input id="firstNumber">
         <button onclick="FirstNum()">Enter</button>
      </p>
      <p id="blockTwo" style="display:none;">Second Number <input id="secondNumber">
         <button onclick="SecondNum()">Add</button>
      </p>
      <p id="blockThree" style="display:none;">Sum = <span id="res"></span></p>
   </body>
</html>


2. Largest of three numbers


<!doctype html>
<html>
   <head>
      <script>
         function largestnum()
      {
        var num1, num2, num3;
        num1 = parseInt(document.getElementById("first").value);
        num2 = parseInt(document.getElementById("second").value);
        num3 = parseInt(document.getElementById("third").value);
        if(num1>num2 && num1>num3)
        {
          window.alert(num1+ "is greatest");
        }
        else if(num2>num1 && num2>num3)
        {
          window.alert(num2+ "is greatst");
        }
        else if(num3>num1 && num3>num1)
        {
          window.alert(num3+ "is greatest");
        }
      }
      </script>
   </head>
   <body>
     <h1>Check largest number </h1>
    <br>
    Enter number 1: <input type="text" id="first"></input><br>
    Enter number 2: <input type="text" id="second"></input><br>
    Enter number 3: <input type="text" id="third"></input><br>
  <button onclick="largestnum()">submit</button>
   </body>
</html>



3. Odd or Even


Method 1:


<!doctype html>
<html>
<body>
<script>
  var num=6;
  if(num%2==0)
    document.write(num + " is an Even Number");
  else
    document.write(num + " is an Odd Number");
</script>
</body>
</html>


Method 2:


<!doctype html>

<html>
<head>
<script>
var num, temp;
function fun()
{
  num = parseInt(document.getElementById("num").value);
  if(num)
  {
    temp = document.getElementById("resPara");
    temp.style.display = "block";
    if(num%2==0)
      document.getElementById("res").innerHTML = "Even";
    else
      document.getElementById("res").innerHTML = "Odd";
  }
}
</script>
</head>
<body>

<p>Enter the Number: <input id="num"><button onclick="fun()">Check</button></p>
<p id="resPara" style="display:none;">It is an <span id="res"></span> Number</p>

</body>
</html>


4. Prime number

Method 1

<!doctype html>
<html>
<body>
<script>
var num, i, chk=0;
num=19;
for(i=2; i<num; i++)
{
  if(num%2==0)
  {
    chk++;
    break;
  }
}
if(chk==0)
  document.write(num + " is a Prime Number");
else
  document.write(num + " is not a Prime Number");
</script>
</body>
</html>


Method 2

<!doctype html>
<html>
<head>
<script>
var num, i, chk, temp;
function checkPrime()
{
  num = parseInt(document.getElementById("num").value);
  if(num)
  {
    chk=0;
    temp = document.getElementById("resPara");
    temp.style.display = "block";
    for(i=2; i<num; i++)
    {
      if(num%2==0)
      {
        chk++;
        break;
      }
    }
    if(chk==0)
      document.getElementById("res").innerHTML = "a Prime";
    else
      document.getElementById("res").innerHTML = "not a Prime";
  }
}
</script>
</head>
<body>

<p>Enter the Number: <input id="num"><button onclick="checkPrime()">Check</button></p>
<p id="resPara" style="display:none;">It is <span id="res"></span> Number</p>

</body>
</html>


5. Add n Number 

<!doctype html>
<html>
<head>
<script>
var i=0, n, sum=0, temp;
function fun()
{
  n = parseInt(document.getElementById("n").value);
  if(n)
  {
    document.getElementById("nVal").innerHTML = n;
    document.getElementById("tme").innerHTML = i+1;
    i++;
    temp = document.getElementById("one");
    temp.style.display = "none";
    temp = document.getElementById("two");
    temp.style.display = "block";
  }
}
function add()
{
  if(i==n)
  {
    temp = document.getElementById("two");
    temp.style.display = "none";
    temp = document.getElementById("three");
    temp.style.display = "block";
  }
  num = parseInt(document.getElementById("num").value);
  if(num)
  {
    sum = sum + num;
    document.getElementById("num").value = null;
    document.getElementById("tme").innerHTML = i+1;
    i++;
  }
}
function res()
{
  document.getElementById("result").value = sum;
}
</script>
</head>
<body>

<p id="one">Enter the Value of n: <input id="n">
<button onclick="fun()">ENTER</button></p>
<p id="two" style="display:none;">Enter <span id="nVal"></span> 
Numbers. Input No. <span id="tme"></span>: 
<input id="num"><button id="btn" onclick="add()">ENTER</button></p>
<p id="three" style="display:none;"><button onclick="res()">Add</button>
<input id="result"></p>

</body>
</html>


6. Fibonacci Series

<!DOCTYPE html>
<html>
<head>
  <title>Fibonacci Series</title>
</head>
<body>
  <script type="text/javascript" >
   <!--
      var var1 = 0;
      var var2 = 1;
      var var3;
      var num = prompt("Enter the limit to generate Fibonacci Series");
        document.write(var1+"<br/>");
        document.write(var2+"<br/>");

        for(var i=3;i<=num;i++)
        {

          var3 = var1+var2;
          var1 = var2;
          var2 = var3;

          document.write(var3+"<br/>");
        }


   //--> 


  </script>
  
</body>
</html>

No comments:

Post a Comment