ADVERTISEMENT
ADVERTISEMENT

Java program to find the absolute value of a number using library method

Given a value, we have to find the absolute value of a number using library method.
Submitted by Nidhi, on May 15, 2022

Problem Solution:

In this program, we will read a floating-point number from the user using the Scanner class and find the absolute value of the given number using the Math.abs() method. The Math.abs() method always returns a positive value.

Program/Source Code:

The source code to find the absolute value of a number using the library method is given below. The given program is compiled and executed successfully.

// Java program to find the absolute value of a number 
// using the library method

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);

    double num = 0;

    System.out.print("Enter number: ");
    num = X.nextDouble();

    System.out.print("Cube root of num is: " + Math.abs(num));
  }
}

Output:

Enter number: -345
Cube root of num is: 345.0

Explanation:

In the above program, we imported the "java.util.*" package to use the Scanner class. Here, we created a public class Main that contains a main() method.

The main() method is the entry point for the program. Here, we created a variable num of double type and read its value from the user using the nextDouble() method of the Scanner class. Then we found the absolute value of the input number using the Math.abs() method and printed the result.

Java Math Class Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.