Java

How to calculate the absolute value in Java

Absolute value is a non-negative value indicating how distant a number is from 0. Absolute value is always positive because it does not signify the direction. The purpose of the Math.abs() function in Java is to make any negative number positive and has no impact on the number if it is already positive. In this article, we will calculate the absolute value using the Java programming language.

How to calculate the absolute value using Math.abs function in Java

We will take input from the user that can be possible using the class java.util.Scanner provides a very simplified and easy way to enable users to input values through the keyboard and for mathematical operations we need to use java.lang.Math:

import java.util.Scanner;
import java.lang.Math;

The next step is to create a Scanner object in the code like the following:

Scanner in=new Scanner(System.in);

Now after that, we have created a public class with the name of AbsoluteValue:

public class AbsoluteValue {
   public static void main(String[] args)  {

Now if you want to print any text then you can do that by typing:

 System.out.println("Java Absolute Value. Example 1");

Now we have used the type of double here as the absolute value can be infraction as well and to take the input as an integer from the user we need to type.

 double n=in.nextDouble();

The input value will be stored in the variable n. Now there is a built-in math function in Java that is used to calculate the absolute value of any object which is

 Math.abs(number);

So in our case, we will be using this function as follow

 double a=Math.abs(n);

In the above line ‘n’ is the variable where a user will get the input and ‘Math.abs’ function is used to calculate the absolute value of the n variable and the result will be stored in the new variable which we have initialized like a variable ‘a’. In the end, we will print the absolute value by typing

 System.out.println("Absolute value of " + n + " is "  + a);

Complete Code

The complete code that we have discussed above is as follow:

import  java.util.Scanner;
import java.lang.Math;
public class AbsoluteValue {
    public static void main(String[] args)    {
        //Define a Scanner object for data input.
        Scanner in=new Scanner(System.in);
        System.out.println("Java Absolute Value. Example 1");
        System.out.println("Please enter a number ");
        double n=in.nextDouble();
        //Use java.lang.Math.abs() to get absolute value of n
        double a=Math.abs(n);
        System.out.println("Absolute value of " + n + " is "  + a);
        System.out.println("____________");
 }
}

You can use any text editor to code java programs in Linux operating system for example we have used nano text editor by typing:

 $ nano AbsoluteValue.java

After that, you need to type and save the code that you want to execute:

Note: You need to install the java development kit (JDK) to execute the java based programs and for that, you need to type:

$ sudo apt install default-jdk

After saving the code you need to compile it first and after that, you can execute it by typing:

$ javac AbsoluteValue.java
$ java AbsoluteValue

How to calculate the absolute value using conditional statements in Java

There is another way of calculating the absolute value if you don’t want to use the built-in math function of absolute value and that is by using a conditional statement. Here we are using the if-else condition and the remaining part of the code is almost the same. So first of all we are printing a message as shown below:

 System.out.println("Please enter a number ");

After that user need to input any number and for that, we need to initialize the input function:

 double p=in.nextDouble();

Now the input number will be stored in a ‘p’ variable which has a double integer type and after that, we are using a conditional statement to convert any number into its absolute (positive) form as shown below:

double b;
if(p<0){
   b = -p;
  }
else {
   b = p;
  }

This condition specifies that if a number p is less than 0 then add that number with a negative sign to make it positive and if the number is already positive then make no changes and at the end, the absolute value will be stored in the variable b:

The complete code that we have discussed above is as follow:

import  java.util.Scanner;
public class AbsoluteValue {
    public static void main(String[] args)    {
        //Define a Scanner object for data input.
        Scanner in=new Scanner(System.in);
        System.out.println("Java Absolute Value. Example 2");
        System.out.println("Please enter a number ");
        double p=in.nextDouble();
        //Use java.lang.Math to get absolute value of n
        double b;
        if(p<0){
            b = -p;
        }
        else {
            b = p;
        }
        System.out.println("Absolute value of " + p + " is "  + b);
        System.out.println("____________");
    }
}

You can see the code in the nano text editor as shown below:

After compiling the code you will get the result as shown below:

Conclusion

Absolute value is a non-negative value indicating how far the number is from 0. In java, there are various ways to calculate the absolute value, and two of them are mentioned in this article. The first one is by using a built-in function with the name of Math.abs(number) and the second one is by using the if-else condition.

About the author

Taimoor Mohsin

Page was generated in 0.062781095504761