JavaScript – Arithmetic Operators

Operator: Operator is a symbol that performs operation on data.

Arithmetic Operators:

  • These operators are used to perform all arithmetic operations.
  • Operators are +, – , *, / , %
  • Division(/) operator returns the quotient.
  • Mod(%) operator returns the remainder.
<!doctype html>
<html>
            <body>
                        <script>
                                    var a=5, b=3;
                                    document.write(a + ” + ” + b + ” = ” + (a+b) + “<br/>”);
                                    document.write(a + ” – ” + b + ” = ” + (a-b) + “<br/>”);
                                    document.write(a + ” * ” + b + ” = ” + (a*b) + “<br/>”);
                                    document.write(a + ” / ” + b + ” = ” + (a/b) + “<br/>”);
                                    document.write(a + ” % ” + b + ” = ” + (a%b) + “<br/>”);
                        </script>
            </body>
</html>

Program to display the last digit of given number:

<!doctype html>
<html>
            <head>
                        <script>
                                    function lastDigit()
                                    {
                                                var num = parseInt(document.getElementById(“num”).value);
                                                var x = num%10;
                                                document.getElementById(“last”).innerHTML=”Last Digit is : “+x;
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”lastDigit()”> Last Digit </button>
                        <p id=”last”> </p>
            </body>
</html>

Program to remove the last digit of given number:

<!doctype html>
<html>
            <head>
                        <script>
                                    function removeLastDigit()
                                    {
                                                var num = parseInt(document.getElementById(“num”).value);
                                                var n = Math.floor(num/10);
                                                document.getElementById(“last”).innerHTML = “Now n is : ” + n ;
                                    }
                        </script>
            </head>
            <body>
                        Enter Number : <input type=”text” id=”num”/>
                        <button onclick=”removeLastDigit()”> Remove Last Digit </button>
                        <p id=”last”> </p>
            </body>
</html>
Scroll to Top