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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s