Exp 1
Exp 1
Aim: Write a JavaScript with HTML for arithmetic expression evaluation and message
printing.
Theory:
1. Arithmetic operators:-
Order of operation: It specify set of rules that indicate order in which an expression is
evaluated by the browser.
3. Assignment operators:-
These operators assign the value from the right side of the operator to the variable on
the left side of the operator.
4. Comparison operators:-
These operators are used to compare two values(operands).
Programs:
1. Arithmetic operators:
Program: Output: -
<html> Arithmetic operators
<head></head>
<body> Addition= 10
<h1>Arithmetic operators</h1> Subtraction =2
<script type="text/javascript"> Multiplication =24
var a,b,c; Division = 1.5
a=6; Modulus = 2
b=4; Pre-increment = 7
c=a+b; Post-increment = 6
document.write("Addition= "+ c + "<br>");
c=a-b;
document.write("Subtraction =" + c +"<br>");
c=a*b;
document.write("Multiplication =" + c +
"<br>");
c=a/b;
document.write("Division = " + c + "<br>");
c=a%b;
document.write("Modulus = " + c + "<br>");
a=6;
c=++a;
document.write("Pre-increment = " + c +
"<br>");
a=6;
c=a++;
document.write("Post-increment = " + c +
"<br>");
</script>
</body>
</html>
2. Logical operators:
Program: Output: -
<html> Logical operators
<head></head>
<body> AND(&&) operator
<h1>Logical operators</h1> a and b both are greater than 0
<script type="text/javascript"> OR(||) operator
var a,b; a is a vowel
a=6; NOT(!) operator
b=4; a and b are not equal
document.write("AND(&&) operator <br>");
if(a>0 && b>0)
{
document.write(" a and b both are greater than
0");
}
document.write("<br> OR(||) operator <br>");
a='i';
if(a=='a'|| a=='e' || a=='i' || a=='u' || a=='o')
{
document.write(" a is a vowel");
}
else
{
document.write(" a is not a vowel");
}
document.write("<br> NOT(!) operator
<br>");
if(!(a==b))
{
document.write("a and b are not equal");
}
else
{
document.write(" a and b are equal");
}
</script>
</body>
</html>
3. Assignment operators:
Program: Output:
<html> Assignment operators
<head></head>
<body> Equalto (=) operator :a=4
<h1>Assignment operators</h1> Add then Assign (+=) operator :b=10
<script type="text/javascript"> Subtract then Assign (-=) operator :b=2
var a,b; Multiplication then Assign (*=)
a=4; operator :b=24
document.write(" Equalto (=) operator :" + Division then Assign (/=) operator :b=1.5
"a=" + a); Modulus then Assign (%=) operator :b=2
document.write(" <br>Add then Assign (+=)
operator :");
b=6;
b+=a;
document.write("b="+b);
document.write(" <br>Subtract then Assign (-
=) operator :");
b=6;
b-=a;
document.write("b="+b);
document.write(" <br>Multiplication then
Assign (*=) operator :");
b=6;
b*=a;
document.write("b="+b);
document.write(" <br>Division then Assign
(/=) operator :");
b=6;
b/=a;
document.write("b="+b);
document.write(" <br>Modulus then Assign
(%=) operator :");
b=6;
b%=a;
document.write("b="+b);
</script>
</body>
</html>
4. Comparison operators:
Program: Output:
<html> Comparison operators
<head></head>
<body> Equal to equal to (==) operator :a and b are
<h1>Comparison operators</h1> not equal
<script type="text/javascript"> Not Equal to (!=) operator :a and b are not
var a,b; equal
a=6; Greater than (>) operator :a is greater than b
b=4; Greater than equal to (>=) operator :a is
document.write(" Equal to equal to (==) greater than or equal to b
operator :"); Less than (<) operator :a is not less than b
if(a==b) Less than equal to (<=) operator :a is not
document.write("a and b are equal<br>"); less than or equal to b
else
document.write("a and b are not equal<br>");
5. Conditional operator:
Program: Output:
<html> Conditional operator
<head></head>
<body> Answer for conditional operator
<h1>Conditional operator</h1> 14
<script type="text/javascript">
var a,b,result;
a=6;
b=14;
result=(a>b)?a:b;
document.write("Answer for conditional
operator <br>" +result);
</script>
</body>
</html>
Conclusion:
With all the concepts based on operators and printing message ,successfully executed all
programs with correct output.