Monday 12 August 2013

multiplication-of-numbers-using-javascript


This post will show a simple Code in JavaScript to Multiply Numbers in a particular range.

If you're a beginner, then first try to run your first program in JavaScript.

Write the following code in any text file like Notepad, Notepad++.
Now, save this file as "amitJavascript.html"


<html>
     <head><title>www.techterabyte.com javascript sample</title></head>
<script>
<!-- <script> tag is used to insert JavaScript into html document. -->

function multiplyNum(n) {
   var i, mul = 1; // variable declaration

   // loop to multiply numbers
   for (i = 5; i <= n; i++) {
      mul = mul*i;              
   }

   // this will print the multiplication of numbers from number 5
   alert("The multiplication of digits from 5 to "+ n + " is:\n\n\t " + mul);
}

</script>

<body>
<p style="font-weight: bold;">
www.techterabyte.com<br />
...Multiplication of numbers...</p>
<form name="MultiplicationForm">
   Multiplication of the digits from 5 to
   <input name="enterNum" type="text" />
   <input onclick="multiplyNum(MultiplicationForm.enterNum.value)" 
   type="button" value="CalculateResult" />
</form>
</body>
</html>


The above can be explained as:
  • <script> : this tag is used to insert JavaScript into html document
  •  !--      --> : these are used for comments


Now. open the above page(amitJavascript.html) under any Web Browser:

Output :(this will shown on the browser as web page)



Enter the maximum value and click on CalculateResult to generate multiplication result:



Through this you can multiply numbers in JavaScript by inserting <script> tag in HTML.




No comments:

Post a Comment