Construct an expression that is equal to the absolute value of a variable.

Q7. Construct an expression that is equal to the absolute value of a 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:

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

Ans.(1). |p|

Ans.(2).  p > 0 ? p : -p

Leave a Reply

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

70 Views
%d bloggers like this: