Expression.

Q5. Construct an expression that is  equal to the absolute value of an variable. That is, if a variable p is positive, the value of the expression is just p, but if  p is negative, the value of the expression is -p, which would be positive. Do it in two ways:

i.) using a mathematical function

ii.) using a conditional operator.(Do not use the mathematical function here).

Ans.

i) Math.abs(p)

ii) p > 0 ? p : -p

Leave a Reply

Your email address will not be published. Required fields are marked *