Thursday, January 23, 2025
HomeProgrammingJava sqrt() Method with Examples

Java sqrt() Method with Examples

Java sqrt() Method with Examples

The sqrt() method in Java is part of the Math class and is used to calculate the square root of a number. It returns a double value and accepts a positive number or zero as input. If the input is negative, it returns NaN.

Syntax:

java
double result = Math.sqrt(number);

Examples:

java
System.out.println(Math.sqrt(16)); // Output: 4.0
System.out.println(Math.sqrt(2)); // Output: 1.4142135623730951
System.out.println(Math.sqrt(-1)); // Output: NaN

This method is commonly used in mathematical calculations, geometry, and scientific applications.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x