Program to Find Square and Square Root in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Squares are the numbers generated when a value is multiplied by itself.

In contrast, a number's square root is a value that, when multiplied by itself, returns the original value. Both are therefore vice-versa methods.

For example, the square of 33 is 99 and the square root of 99 is 33.

In Java Math. sqrt() returns the square root of a value of type double passed to it as argument.(There are other ways as well)

As a programmer, I always find it fascinating to write programs for mathematical operations that I used to do by hand back in school.

Square and square root of a number is one of the principles which are used in many real-life applications as well as programming concepts. For example prime factors of a number, binary exponentiation etc.

Let’s start by defining what a square and square root actually means.

What is Square?

Simply put, the square of a number is the number multiplied by itself.

Square of X=XXX = X * X

For example, if X=3X = 3

Square of 3=33=93 = 3 * 3 = 9

What is Square Root?

Square root is exactly the opposite of the square of a number. The square root of a number X is the number that when multiplied by itself equals X.

Square root of X = √X

where is the symbol for square root

For example, if X = 9

Square root of 9 = √9 = 3

The square root of X can also be represented by X1/2X^{1/2}.

In a nutshell, if the square of a number X is S, then X is the square root of S.

Now that we know what we mean by the square and square root of a number, let’s see how we can write a program for square root in java.

How to square a number in Java?

There are a couple of ways to find the square of a number in Java. Let’s look at them one by one.

1. Multiplying the number by itself

It’s as simple as it sounds. Just multiply the number by itself and you’re good to go.

2. Using Math.pow Method

Math is a utility class in Java that has many useful mathematical functions, one of which is pow which allows you to calculate the exponentiation or power of a number.

Syntax:

Parameters : base, exponent Returns: base to the power of exponent

Both parameters accept double as the input. The return type is also a double value.

If you pass an integer or any primitive numerical value to either base or exponent, it will automatically be cast to double in Java.

So, if base = 10, exponent = 2 Math.pow(10, 2) = 100.0

So, in order to find the square of a number, we can pass the base as the number and the exponent as 2. Here’s a snippet that shows the usage of Math.pow.

How to find Square Root in Java?

Calculating the square of a number was pretty straightforward right. But calculating the Square Root in Java is a bit more interesting. Let’s see how we can do that.

1. Square Root in Java Using Math.sqrt()

This is the friendly sibling of the Math.pow function we saw in the previous segment. Like the way it reads, it finds the square root of a number.

Syntax

Parameters : number Returns: The square root of number

The number parameter accepts double as input and the return type is also a double value.

So, if number = 9 Math.sqrt(9) = 3.0

Here’s a snippet that demonstrates the use of Math.sqrt()

2. Square Root in Java Using Math.pow()

Just like we used Math.pow to find the square of a number, we can use it to find the square root of a number as well.

But how?

Remember the way the square root of a number is represented: X1/2X^{1/2}; it means the square root of a number is X to the power of ½.

So if we pass the value of the exponent in Math.pow as 0.50.5 which is KaTeX parse error: Expected 'EOF', got '½' at position 1: ½̲, the resulting number will be the square root. Let’s try that in the below snippet.

Neat right? The same function is used to calculate two different operations.

3. Square Root in Java Without using any inbuilt function

Now that we have looked at a few ways to find the square root of a number using inbuilt functions in Java, let’s look at a way without using any such inbuilt function.

Let me propose an algorithm first, and then we’ll break it down one step at a time.

  1. Start from i=1i = 1, if ii==ni * i == n, then i is the square root of n as n is a perfect square.

  2. if ii>ni * i > n, it means the square root must lie between (i1,i)(i-1, i), let’s call them (low,high)(low, high).

  3. Apply binary search in the range (low,high)(low, high). Find midmid of (low,high)(low, high):

  4. if midmid==nmid * mid == n, it means we assume that midmid is the square root of nn.

  5. if midmid>nmid * mid > n, it means that the assumption we made is incorrect. This implies, the square root of n must be less than mid since any value higher than mid can never satisfy the condition midmid==nmid * mid == n. Hence, we will try to find the square root on the left side of mid by repeating step 3 for (low,mid)(low, mid).

  6. Otherwise, midmid<nmid * mid < n means that our assumption is incorrect. It implies the square root of nn must be greater than mid since any value less than mid can never satisfy the condition midmid==nmid * mid == n. Hence, we will try to find the square root on the right side of midmid by repeating step 3 for (mid,high)(mid, high).

What is Perfect Square?

A perfect square is an integer that is the square of an integer. For example,

X=9=33=32X = 9 = 3 * 3 = 3^{2}

Here, 9 is a perfect square because 99 is the square of 33. On the other hand, 1010 is not a perfect square because it cannot be represented as the square of an integer.

So, if we want to calculate the square root of XX, which is a perfect square, we need to find a number that, when multiplied by itself, equals XX.

Numbers that are not a perfect square will have a real number (decimal) as their square root. So, we need a way to find the nearest approximate value for the square root (up to 6 decimal places for now). It’ll be something like: R.abcdef

Let’s say we need to find the square root of 1313, which is not a perfect square. Let’s find R first.

Observe that the maximum perfect square less than 1313 is 99 whose square root is 33. Since the square of 44 is 1616, which is greater than 1313, the square root of 1313 has to be less than 44. So we can be sure that the square root of 1313 as well will be something like 3.abcdef3.abcdef.

Now that we have the value of R(3)R(3) let’s find abcdef.

In order to find the decimal places, we can use Binary Search. We know that the square root will lie between (33, 44). We apply binary search in this range to find the value whose square is equal to 1313 (up to 66 decimal places). Here is a sample walkthrough of how we are applying binary search.

Hence, the square root of 13=3.60555113 = 3.605551

Here is a snippet of this algorithm

Conclusion

  • Squares are the numbers generated after multiplying a value by itself. In contrast, the square root of a number is a value which, on getting multiplied by itself, gives the original value.
  • We can square a number in Java in two different ways, multiplying the number by itself and calling the Math.pow function.
  • We can find sqaure root in Java by using Math.sqrt function, using Math.pow function, or calculating it using a binary search for the given range.
  • A perfect square is an integer that is the square of an integer.

Exercise

  • What will happen if a negative number is passed as the exponent parameter to the Math.pow function?
  • What will happen if a negative number is passed to the Math.sqrt function?
  • Can you think of improving the time complexity of the binary search algorithm we used above to find the square root of a number?

See More