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:

convert 

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete