Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 6

Return Absolute Value

Easy

10 minute session

Summary

Compute the absolute value of a number using conditional logic (no built-in abs()).

Problem statement

Write a function abs_value(x) that returns the absolute value of x. Do not use the built-in abs() function; instead use if/else to handle negative values and zero.

Task

Implement abs_value(x) that returns the non-negative magnitude of x.

Examples

Positive input

Input

abs_value(7)

Output

7

Explanation

7 is already non-negative, so return 7.

Input format

A single number x passed as a function argument: abs_value(x).

Output format

Return the absolute value of x (int or float).

Constraints

x is an int or float. Avoid using built-in abs().

Samples

Sample input 0

abs_value(-3)

Sample output 0

3

Explanation 0

Absolute value of -3 is 3.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

02:27 AM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.