Computer Science, asked by anindyaadhikari13, 1 month ago

Interesting question!

Language: Java.

Create a function that returns the absolute value of a number.

You cannot use abs() function, if-else, switch case or ternary operator in your co‎de.

Hint! - Use replace().​

Answers

Answered by kajalpal1975
9

Answer:

public static int abs(int n) {

return Integer.parseInt(String.valueOf(n).replace("-", ""));

}

Explanation:

String.valueOf(n) returns the String representation of n. Minus sign is replaced with empty String and the String is converted to integer. The integer is returned.

Please mark me as the brainliest.


anindyaadhikari13: Excellent! Keep it up!
Similar questions