import java.util.*;class Main{
// Constant to represent infinity
static final int INFINITY = 9999999;// Function to find the square root of a number by using long division method
static int sqrtByLongDivision(int number) {
if (number < 0) {
throw new IllegalArgumentException(“Input must be a non-negative integer.”);
}if (number == 0) {
return 0; // Square root of 0 is 0
}
int segmentIndex = 0, unitsDigit, segmentIterator; // Loop counters
int currentDivisor = 0;
int unitsDigitOfQuotient = 0;
int currentQuotient = 0;
int currentDividend = 0;
int currentRemainder = 0;
int segments[] = new int[10]; // Array to store segments of the number// Dividing the number into segments of two digits
while (number > 0) {
segments[segmentIndex] = number % 100; // Extracting the last two digits
number = number / 100; // Removing the last two digits
segmentIndex++;
}// Last index of the array of segments
segmentIndex–;// Start long division from the last segment (segmentIterator = segmentIndex)
for (segmentIterator = segmentIndex; segmentIterator >= 0; segmentIterator–) {// Initializing the remainder to the maximum value
currentRemainder = INFINITY;// Including the next segment in the new dividend
currentDividend = currentDividend * 100 + segments[segmentIterator];// Loop to check for the perfect square closest to each segment
for (unitsDigit = 0; unitsDigit <= 9; unitsDigit++) {// This condition is to find the divisor after adding a digit
// in the range 0 to 9
if (currentRemainder >= currentDividend
– ((currentDivisor * 10 + unitsDigit) * unitsDigit)
&& currentDividend
– ((currentDivisor * 10 + unitsDigit) * unitsDigit)
>= 0) {// Calculating the remainder
currentRemainder = currentDividend – ((currentDivisor * 10
+ unitsDigit)
* unitsDigit);// Updating the units digit of the quotient
unitsDigitOfQuotient = unitsDigit;
}
}// Adding the units digit to the quotient
currentQuotient = currentQuotient * 10
+ unitsDigitOfQuotient;// New divisor is two times the quotient
currentDivisor = currentQuotient * 2;// Including the remainder in the new dividend
currentDividend = currentRemainder;
}return currentQuotient;
}// Driver code
public static void main(String[] args) {
int inputNumber = 1225;
System.out.print(“Square root of ” + inputNumber + ” is: ” + sqrtByLongDivision(inputNumber) + “\n”);
}
}