Long Division Method to find Square root with Examples
Last Updated :
26 Nov, 2021
Given an integer X which is a perfect square, the task is to find the square root of it by using the long division method.
Examples:
Input: N = 484
Output: 22
222 = 484
Input: N = 144
Output: 12
122 = 144
Approach:
Long division is a very common method to find the square root of a number. The following is the stepwise solution for this method:
1. Divide the digits of the number into pairs of segments starting with the digit in the units place. Let's identify each pair and the remaining final digit(in case there is an odd count of digits in the number) as a segment.
For example:
1225 is divided as (12 25)
2. After dividing the digits into segments, start from the leftmost segment. The largest number whose square is equal to or just less than the first segment is taken as the divisor and also as the quotient (so that the product is the square).
For example:
9 is the closest perfect square to 12, the first segment 12
3. Subtract the square of the divisor from the first segment and bring down the next segment to the right of the remainder to get the new dividend.
For example:
12 - 9 = 3 is concatenated with next segment 25.
New dividend = 325
4. Now, the new divisor is obtained by taking two times the previous quotient(which was 3 in the above example as 32 = 9) and concatenating it with a suitable digit which is also taken as the next digit of the quotient, chosen in such a way that the product of the new divisor and this digit is equal to, or just less than the new dividend.
For example:
Two times quotient 3 is 6.

65 times 5 is 325 which is closest to the new dividend.
5. Repeat steps (2), (3) and (4) till all the segments have been taken up. Now, the quotient so obtained is the required square root of the given number.
Below is the implementation of the above approach:
CPP
// C++ program to find the square root of a
// number by using long division method
#include <bits/stdc++.h>
using namespace std;
#define INFINITY_ 9999999
// Function to find the square root of
// a number by using long division method
int sqrtByLongDivision(int n)
{
int i = 0, udigit, j; // Loop counters
int cur_divisor = 0;
int quotient_units_digit = 0;
int cur_quotient = 0;
int cur_dividend = 0;
int cur_remainder = 0;
int a[10] = { 0 };
// Dividing the number into segments
while (n > 0) {
a[i] = n % 100;
n = n / 100;
i++;
}
// Last index of the array of segments
i--;
// Start long division from the last segment(j=i)
for (j = i; j >= 0; j--) {
// Initialising the remainder to the maximum value
cur_remainder = INFINITY_;
// Including the next segment in new dividend
cur_dividend = cur_dividend * 100 + a[j];
// Loop to check for the perfect square
// closest to each segment
for (udigit = 0; udigit <= 9; udigit++) {
// This condition is to find the
// divisor after adding a digit
// in the range 0 to 9
if (cur_remainder >= cur_dividend
- ((cur_divisor * 10 + udigit)
* udigit)
&& cur_dividend
- ((cur_divisor * 10 + udigit) * udigit)
>= 0) {
// Calculating the remainder
cur_remainder = cur_dividend - ((cur_divisor * 10
+ udigit)
* udigit);
// Updating the units digit of the quotient
quotient_units_digit = udigit;
}
}
// Adding units digit to the quotient
cur_quotient = cur_quotient * 10
+ quotient_units_digit;
// New divisor is two times quotient
cur_divisor = cur_quotient * 2;
// Including the remainder in new dividend
cur_dividend = cur_remainder;
}
return cur_quotient;
}
// Driver code
int main()
{
int x = 1225;
cout << sqrtByLongDivision(x) << endl;
return 0;
}
Java
// Java program to find the square root of a
// number by using long division method
import java.util.*;
class GFG{
static final int INFINITY_ =9999999;
// Function to find the square root of
// a number by using long division method
static int sqrtByLongDivision(int n)
{
int i = 0, udigit, j; // Loop counters
int cur_divisor = 0;
int quotient_units_digit = 0;
int cur_quotient = 0;
int cur_dividend = 0;
int cur_remainder = 0;
int a[] = new int[10];
// Dividing the number into segments
while (n > 0) {
a[i] = n % 100;
n = n / 100;
i++;
}
// Last index of the array of segments
i--;
// Start long division from the last segment(j=i)
for (j = i; j >= 0; j--) {
// Initialising the remainder to the maximum value
cur_remainder = INFINITY_;
// Including the next segment in new dividend
cur_dividend = cur_dividend * 100 + a[j];
// Loop to check for the perfect square
// closest to each segment
for (udigit = 0; udigit <= 9; udigit++) {
// This condition is to find the
// divisor after adding a digit
// in the range 0 to 9
if (cur_remainder >= cur_dividend
- ((cur_divisor * 10 + udigit)
* udigit)
&& cur_dividend
- ((cur_divisor * 10 + udigit) * udigit)
>= 0) {
// Calculating the remainder
cur_remainder = cur_dividend - ((cur_divisor * 10
+ udigit)
* udigit);
// Updating the units digit of the quotient
quotient_units_digit = udigit;
}
}
// Adding units digit to the quotient
cur_quotient = cur_quotient * 10
+ quotient_units_digit;
// New divisor is two times quotient
cur_divisor = cur_quotient * 2;
// Including the remainder in new dividend
cur_dividend = cur_remainder;
}
return cur_quotient;
}
// Driver code
public static void main(String[] args)
{
int x = 1225;
System.out.print(sqrtByLongDivision(x) +"\n");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find the square root of a
# number by using long division method
INFINITY_ = 9999999
# Function to find the square root of
# a number by using long division method
def sqrtByLongDivision(n):
i = 0
udigit, j = 0, 0 # Loop counters
cur_divisor = 0
quotient_units_digit = 0
cur_quotient = 0
cur_dividend = 0
cur_remainder = 0
a = [0]*10
# Dividing the number into segments
while (n > 0):
a[i] = n % 100
n = n // 100
i += 1
# Last index of the array of segments
i -= 1
# Start long division from the last segment(j=i)
for j in range(i, -1, -1):
# Initialising the remainder to the maximum value
cur_remainder = INFINITY_
# Including the next segment in new dividend
cur_dividend = cur_dividend * 100 + a[j]
# Loop to check for the perfect square
# closest to each segment
for udigit in range(10):
# This condition is to find the
# divisor after adding a digit
# in the range 0 to 9
if (cur_remainder >= cur_dividend
- ((cur_divisor * 10 + udigit)
* udigit)
and cur_dividend
- ((cur_divisor * 10 + udigit) * udigit)
>= 0):
# Calculating the remainder
cur_remainder = cur_dividend - ((cur_divisor * 10
+ udigit)
* udigit)
# Updating the units digit of the quotient
quotient_units_digit = udigit
# Adding units digit to the quotient
cur_quotient = cur_quotient * 10 + quotient_units_digit
# New divisor is two times quotient
cur_divisor = cur_quotient * 2
# Including the remainder in new dividend
cur_dividend = cur_remainder
return cur_quotient
# Driver code
x = 1225
print(sqrtByLongDivision(x))
# This code is contributed by mohit kumar 29
C#
// C# program to find the square root of a
// number by using long division method
using System;
class GFG
{
static readonly int INFINITY_ =9999999;
// Function to find the square root of
// a number by using long division method
static int sqrtBylongDivision(int n)
{
int i = 0, udigit, j; // Loop counters
int cur_divisor = 0;
int quotient_units_digit = 0;
int cur_quotient = 0;
int cur_dividend = 0;
int cur_remainder = 0;
int []a = new int[10];
// Dividing the number into segments
while (n > 0) {
a[i] = n % 100;
n = n / 100;
i++;
}
// Last index of the array of segments
i--;
// Start long division from the last segment(j=i)
for (j = i; j >= 0; j--) {
// Initialising the remainder to the maximum value
cur_remainder = INFINITY_;
// Including the next segment in new dividend
cur_dividend = cur_dividend * 100 + a[j];
// Loop to check for the perfect square
// closest to each segment
for (udigit = 0; udigit <= 9; udigit++) {
// This condition is to find the
// divisor after adding a digit
// in the range 0 to 9
if (cur_remainder >= cur_dividend
- ((cur_divisor * 10 + udigit)
* udigit)
&& cur_dividend
- ((cur_divisor * 10 + udigit) * udigit)
>= 0) {
// Calculating the remainder
cur_remainder = cur_dividend - ((cur_divisor * 10
+ udigit)
* udigit);
// Updating the units digit of the quotient
quotient_units_digit = udigit;
}
}
// Adding units digit to the quotient
cur_quotient = cur_quotient * 10
+ quotient_units_digit;
// New divisor is two times quotient
cur_divisor = cur_quotient * 2;
// Including the remainder in new dividend
cur_dividend = cur_remainder;
}
return cur_quotient;
}
// Driver code
public static void Main(String[] args)
{
int x = 1225;
Console.Write(sqrtBylongDivision(x) +"\n");
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program to find the square root of a
// number by using long division method
let INFINITY_ =9999999;
// Function to find the square root of
// a number by using long division method
function sqrtByLongDivision(n)
{
let i = 0, udigit, j; // Loop counters
let cur_divisor = 0;
let quotient_units_digit = 0;
let cur_quotient = 0;
let cur_dividend = 0;
let cur_remainder = 0;
let a = new Array(10);
// Dividing the number into segments
while (n > 0) {
a[i] = n % 100;
n = Math.floor(n / 100);
i++;
}
// Last index of the array of segments
i--;
// Start long division from the last segment(j=i)
for (j = i; j >= 0; j--) {
// Initialising the remainder to the maximum value
cur_remainder = INFINITY_;
// Including the next segment in new dividend
cur_dividend = cur_dividend * 100 + a[j];
// Loop to check for the perfect square
// closest to each segment
for (udigit = 0; udigit <= 9; udigit++) {
// This condition is to find the
// divisor after adding a digit
// in the range 0 to 9
if (cur_remainder >= cur_dividend
- ((cur_divisor * 10 + udigit)
* udigit)
&& cur_dividend
- ((cur_divisor * 10 + udigit) * udigit)
>= 0) {
// Calculating the remainder
cur_remainder = cur_dividend - ((cur_divisor * 10
+ udigit)
* udigit);
// Updating the units digit of the quotient
quotient_units_digit = udigit;
}
}
// Adding units digit to the quotient
cur_quotient = cur_quotient * 10
+ quotient_units_digit;
// New divisor is two times quotient
cur_divisor = cur_quotient * 2;
// Including the remainder in new dividend
cur_dividend = cur_remainder;
}
return cur_quotient;
}
// Driver code
let x = 1225;
document.write(sqrtByLongDivision(x) +"<br>");
// This code is contributed by unknown2108
</script>
Time Complexity: O((log100n)2 * 10)
Auxiliary Space: O(1)
Similar Reads
Babylonian method for square root
Algorithm: This method can be derived from (but predates) NewtonâRaphson method. 1 Start with an arbitrary positive start value x (the closer to the root, the better). 2 Initialize y = 1. 3. Do following until desired approximation is achieved. a) Get the next approximation for root using average of
9 min read
Square root of two Complex Numbers
Given two positive integers A and B representing the complex number Z in the form of Z = A + i * B, the task is to find the square root of the given complex number. Examples: Input: A = 0, B =1Output:The Square roots are: 0.707107 + 0.707107*i-0.707107 - 0.707107*i Input: A = 4, B = 0Output:The Squa
9 min read
Sum of elements whose square root is present in the array
Given an array arr[], the task is to find the sum of all those elements from the given array whose square root is present in the same array. Examples: Input: arr[] = {1, 2, 3, 4, 6, 9, 10} Output: 13 4 and 9 are the only numbers whose square roots 2 and 3 are present in the array Input: arr[] = {4,
9 min read
Square root of a number using log
For a given number find the square root using log function. Number may be int, float or double. Examples: Input : n = 9Output : 3 Input : n = 2.93Output : 1.711724 We can find square root of a number using sqrt() method: C++ // C++ program to demonstrate finding // square root of a number using sqrt
3 min read
Square root of a number without using sqrt() function
Given a number N, the task is to find the square root of N without using sqrt() function. Examples: Input: N = 25 Output: 5 Input: N = 3 Output: 1.73205 Input: N = 2.5 Output: 1.58114 Approach 1: We can consider (?x-?x)2 = 0.Replacing one of the ?x's with y, then the equation becomes (y-?x)2 => y
14 min read
Find square root of a number using Bit Manipulation
Given a non-negative integer N, the task is to find the square root of N using bitwise operations. If the integer is not the perfect square, return largest integer that is smaller than or equal to square root of N i.e., floor(âN). Examples: Input: N = 36Output: 6Explanation: The square root of 36 is
7 min read
Squareroot(n)-th node in a Linked List
Given a Linked List, write a function that accepts the head node of the linked list as a parameter and returns the value of node present at (floor(sqrt(n)))th position in the Linked List, where n is the length of the linked list or the total number of nodes in the list. Examples: Input: 1->2->
9 min read
Check if a number is perfect square without finding square root
Check whether a number is a perfect square or not without finding its square root. Examples: Input: n = 36 Output: Yes Input: n = 12 Output: No Recommended PracticeCheck perfect squareTry It! Method 1:The idea is to run a loop from i = 1 to floor(sqrt(n)) and then check if squaring it makes n. Below
9 min read
Program to calculate Root Mean Square
Given an array of N numbers. The task is to calculate the Root Mean Square(RMS) of the given numbers. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 3.31662 Input: arr[] = {10, 4, 6, 8} Output: 7.34847 Approach: The Root Mean Square value of N numbers x1,x2,x3,.....xn can be given as, RMS method f
4 min read
Finding the Missing Digit in the Largest Perfect Square
Given a perfect square number with one missing digit represented by an underscore (_). Find the missing digit that when inserted, creates the largest perfect square. Examples: Input: "14_"Output: 4Explanation: The missing digit 4 makes the largest perfect square, 144. Input: 6_25Output: 1Explanation
5 min read