Java - Convert negative number to positive
In this section, we will write a Java program to convert negative number to positive (absolute value) using Math.abs().
public class Main {
public static void main(String []args)
{
//Example 1
int sum = 2 + 2 + 2 + 2 + Math.abs(-2);
System.out.println("Sum (absolute value) : " + sum);
//Example 2
int num1 =-9;
int num2 = Math.abs(num1);
System.out.println(num2);
}
}
Console Output:
Total 1 (absolute value) : 10 9
More related topics,
convert