Java Math Square Root Example
How to Calculate Square and Square Root in Java?
One of the most popular frequently asked Java Interview Questions is, "Given an integer x, write a java program to find the square root of it". There are many ways to solve this problem. In this article, let's check out different ways to find square and square root in Java.
- What is Square and Square Root?
2. How to Square a Number in Java
- By multiplying the number by itself
- Using the Math.pow function
3. How to Find Square Root of a Number in Java
- Using java.lang.Math.sqrt() method
- By using Math.pow() function
- Without using any inbuilt functions
Before discussing the square root code in Java, let's understand the term square root first.
Square and Square Root
The square of a number is that number times itself. In other terms, when we multiply an integer by itself we call the product the square of the number. Mathematically, the square of a number is given as,
Square of n = n*n
For example, the square of number 4 is 4*4 = 16
The square root is just the opposite of the square. The square root of a number, n, is the number that gives n when multiplied by itself. Mathematically, the square root of a number is given as,
Square Root of n = √ n
Now that you know what square and square root of a number are, let's see different ways to calculate them in Java.
How to Square a Number in Java?
You can square a number in Java in two different ways:
- Multiply the number by itself
- Call theMath.powfunction
Method 1: Square a number by multiplying it by itself
Here's a Java Program to square a number by multiplying it by itself.
package MyPackage;
import java.util.Scanner;
public class Square1 {
public static void main(String args[]) {
Double num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
num=sc.nextDouble();
Double square = num*num;
System.out.println("Square of "+ num + " is: "+ square);
}
}
Output
Enter a number: 10 Square of 10.0 is: 100.0
Method 2: Square a number with the Math.pow method
Here's a Java Program to call the Math.pow method to square a number.
package MyPackage;
import java.util.Scanner;
import java.lang.Math;
public class Square2 {
public static void main(String args[]) {
Double num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextDouble();
Double square = Math.pow(num, 2);
System.out.println("Square of "+ num + " is: "+ square);
}
}
Output
Enter a number: 22
Square of 22.0 is: 484.0
Now let's check out how to calculate the square root of a number in Java.
How to Find Square Root of a Number in Java
There are multiple ways to find square root a given number in Java. Let's explore a few of those.
Method 1: Java Program to Find the square root of a Number using java.lang.Math.sqrt() method
Syntax
public static double sqrt(double x)
- Parameter: x is the value whose square root is to be returned.
2. Return: This method returns the square root value of the argument passed to it.
- If parameter x is positive double value, this method will return the square root of x
- When x is NaN or less than zero, this method will return NaN
- If parameter x is positive infinity, this method will return positive Infinity
- When x is positive or negative zero, this method will return the result as Zero with the same sign
Code
package MyPackage;
public class SquareRoot2
{
public static void main(String args[])
{
double a = 100;
System.out.println(Math.sqrt(a));
// Input positive value, Output square root of x
double b = -81.00;
System.out.println(Math.sqrt(b));
// Input negative value, Output NaN
double c = 0.0/0;
// Input NaN, Output NaN
System.out.println(Math.sqrt(c));
double d = 1.0/0;
// Input positive infinity, Output positive infinity
System.out.println(Math.sqrt(d));
double e = 0.0;
// Input positive Zero, Output positive zero
System.out.println(Math.sqrt(e));
}
}
Output
10.0
NaN
NaN
Infinity
0.0
Method 2: Java Program to Find the square root of a Number using java.lang.Math.pow() method
We can use the logic √number = number½ to find the square root of a number.
Code
package MyPackage;
import java.util.Scanner;
public class SquareRoot1 {
public static void main(String[] args)
{
Double num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextDouble();
Double squareroot = Math.pow(num, 0.5);
System.out.println("The Square of a Given Number " + num + " = " + squareroot);
}
}
Output
Enter a number: 81
The Square of a Given Number 81.0 = 9.0
Method 3: Java Program to Find the square root of a Number without using any in-built method
Here's the logic that we are using:
The first sqrt number should be the input number / 2. Here's a Java Program implementing the above logic.
Code
package MyPackage;
public class SquareRoot
{
public static double square(double number){
double t;
double squareroot = number / 2;
do
{
t = squareroot;
squareroot = (t + (number / t)) / 2;
}
while ((t - squareroot) != 0);
return squareroot;
}
public static void main(String[] args)
{
double number = 16;
double root;
root = square(number);
System.out.println("Number : "+number);
System.out.println("Square Root : "+root);
}
}
Output
Number : 121.0
Square Root : 11.0
This brings us to the end of this article.
Make sure you practice as much as possible and revert your experience.
If you wish to check out more articles on the market's most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka's official site.
Do look out for other articles in this series which will explain the various other aspects of Java.
1. Object Oriented Programming
2. Java Tutorial
3. Polymorphism in Java
4. Abstraction in Java
5. Java String
6. Java Array
7. Java Collections
8. Java Threads
9. Introduction to Java Servlets
10. Servlet and JSP Tutorial
11. Exception Handling in Java
12. Advanced Java Tutorial
13. Java Interview Questions
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
17. Comparable in Java
18. Top 10 Java frameworks
19. Java Reflection API
20. Top 30 Patterns in Java
21. Core Java Cheat Sheet
22. Socket Programming In Java
23. Java OOP Cheat Sheet
24. Annotations in Java
25. Library Management System Project in Java
26. Trees in Java
27. Machine Learning in Java
28. Top Data Structures & Algorithms in Java
29. Java Developer Skills
30. Top 55 Servlet Interview Questions
31. Top Java Projects
32. Java Strings Cheat Sheet
33. Nested Class in Java
34. Java Collections Interview Questions and Answers
35. How to Handle Deadlock in Java?
36. Top 50 Java Collections Interview Questions You Need to Know
37. What is the concept of String Pool in Java?
38. What is the difference between C, C++, and Java?
39. Palindrome in Java- How to check a number or string?
40. Top MVC Interview Questions and Answers You Need to Know
41. Top 10 Applications of Java Programming Language
42. Deadlock in Java
43. Square and Square Root in Java
44. Typecasting in Java
45. Operators in Java and its Types
46. Destructor in Java
47. Binary Search in Java
48. MVC Architecture in Java
49. Hibernate Interview Questions And Answers
Source: https://medium.com/edureka/java-sqrt-method-59354a700571
0 Response to "Java Math Square Root Example"
Post a Comment