Posts

Showing posts with the label Math.abs()

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, Java - Convert comma-separated String to a List and List to a comma-separated String Java - Convert hyphen-separated String to a List and List to a hyphen-separated String Java - Convert colon-separated String to a List and List to a colon-separated String Java - How to get current timestamps Java - How to count duplicated items in a List Java – How to Convert Integer to Long Java - Program to Find Largest Element in an Array Java - How to loop an enum