How to Calculate Square and Square Root in Java?

Last updated on Jul 05,2024 136.6K Views

How to Calculate Square and Square Root in Java?

edureka.co

One of the most popular frequently asked Java Interview Question 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.

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:

  1. Multiply the number by itself
  2. Call the Math.pow function

Method1: 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

Method2: 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

Java Full Course – 10 Hours | Java Full Course for Beginners | Java Tutorial for Beginners | Edureka

🔥𝐄𝐝𝐮𝐫𝐞𝐤𝐚 𝐉𝐚𝐯𝐚 𝐂𝐨𝐮𝐫𝐬𝐞 𝐓𝐫𝐚𝐢𝐧𝐢𝐧𝐠: https://www.edureka.co/java-j2ee-training-course (Use code “𝐘𝐎𝐔𝐓𝐔𝐁𝐄𝟐𝟎”)
This Edureka Java Full Course will help you understand the various fundamentals of Java programm…

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.

Method1: Java Program to Find the square root of a Number using java.lang.Math.sqrt() method

Syntax

public static double sqrt(double x)

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

Method2: 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

Method3: 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.  

Check out the Java Certification Course Online by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.

Got a question for us? Please mention it in the comments section of this ‘Java sqrt() Method’ article and we will get back to you as soon as possible or you can also join Java Training in Dubai.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES