bluej programs to input two unequal positive numbers and check whether they are perfect square numbers or not. if the user enters a negative number then the program displays the message 'Square root of a negative number cannot be determined'.
Answers
Answered by
5
Hey There
// A Java program to find floor(sqrt(x)
public class squareroot
{
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
int start = 1, end = x, ans=0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a perfect square
if (mid*mid == x)
return mid;
// Since we need floor, we update answer when mid*mid is
// smaller than x, and move closer to sqrt(x)
if (mid*mid < x)
{
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid - 1;
}
return ans;
}
// Driver Method
public static void main(String args[])
{
int x = 11;
System.out.println(floorSqrt(x));
}
}
// A Java program to find floor(sqrt(x)
public class squareroot
{
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
int start = 1, end = x, ans=0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a perfect square
if (mid*mid == x)
return mid;
// Since we need floor, we update answer when mid*mid is
// smaller than x, and move closer to sqrt(x)
if (mid*mid < x)
{
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid - 1;
}
return ans;
}
// Driver Method
public static void main(String args[])
{
int x = 11;
System.out.println(floorSqrt(x));
}
}
Answered by
4
Answer:
Your answer is in the ATTACHMENT
Attachments:
Similar questions