Absolute value in java – Java Program to Convert an Integer Value to Absolute Value

Absolute value in java: In the previous article, we have seen Java Program to Multiply Two Numbers

In this article we are going to see how to find absolute value of an integer value using Java programming language.

Java Program to Convert an Integer Value to Absolute Value

Java abs value: Before jumping into the program let’s know what is this absolute value first.

Absolute value:

In mathematics, the absolute value or modulus of a real number x is x’s numeric value without regard to its sign.

  • |x| = x if x is positive
  • |x| = −x if x is negative (Here, (-x) is non negative value)
  • |0| = 0.

For example,

The absolute value of 5 is 5 and

The absolute value of −5 is also 5.

Let’s see different ways to find absolute value of an integer value.

Method-1: Java Program to Convert an Integer Value to Absolute Value By Using Math.abs() Method

Absolute value java: In Java we have Math.abs() method in java.lang.Math class which can be used to find the absolute value of a number. In this approach we will use this inbuilt Math.abs() method to find the absolute value of integer number.

Approach:

  • Initialize an integer variable ‘a’ with a value of -10.
  • Use Math.abs() method to find the absolute value of a.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a number declared
        int a = -10;
        //finding absolute value using Math.abs(a) and printing the result
        System.out.println("Absolute value of " + a + " is " + Math.abs(a));
    }
}
Output:

Absolute value of -10 is 10

Method-2: Java Program to Convert an Integer Value to Absolute Value By Using If-else Statement

Approach:

  1. Initialize an integer variable with a value -10.
  2. Check if the value of the variable is negative i.e., less than zero, to get the absolute value, print the negative of the number.
  3. Else if it is positive, print the number as it is.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a number declared
        int a = -10;
        
        //If 'a' value is less than zero then result is -a
        if (a < 0) 
        {
            System.out.println("Absolute value of " + a + " is " + -a);
        }
        //else result is number itself
        else 
        {
            System.out.println("Absolute value of " + a + " is " + a);
        }
    }
}
Output:

Absolute value of -10 is 10

Method-3: Java Program to Convert an Integer Value to Absolute Value By Using Ternary Operator

Approach:

  • Initialize an integer variable with initial value as 10.
  • Use the ternary operator to find the absolute value.
  • Here the condition we have to check is whether the given value is greater than 0 or not. So put that before the question mark.
  • If it’s greater than 0 i.e., positive, we’ve to return the value as it is so simply write the variable name in place of expression one.
  • If it’s less than 0 i.e., negative, we’ve to return the negative of that value (negative of negative yields positive value)
  • Print the value.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a number declared
        int a = -10;
        
        System.out.print("Absolute value of " + a);
        //Find absolute value using ternary operator
        a = a > 0 ? a : -a;
        System.out.println(" is " + a);
    }
}
Output:

Absolute value of -10 is 10

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: