Question

In Java, Write a program without using a build in square-root operator to compute square-root of...

In Java, Write a program without using a build in square-root operator to compute square-root of a number that is known to have an integer square root

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package math;

public class FindSquireRoot {

public static int findSquireRoot(int n){

int l=1,s=n; //start from 1 to upto that no

if(n==0 || n==1) //if n is zero or 1 return same no

return n;

while(l<=s){ //Iterate through both crosses

int mid=(l+s)/2; //calculate mid

if(mid*mid==n) //if mid*mid==n this is root

return mid;

else if(mid*mid>n){ //mid*mid>n decrement to mid -1

s=mid-1;

}

else{ //mid*mid<n increment to mid +1

l=mid+1;

}

}

return -1;

}

//driver program to test

public static void main(String[] args) {

System.out.println(findSquireRoot(225));

System.out.println(findSquireRoot(81));

System.out.println(findSquireRoot(9));

}

}

output

15

9

3

Add a comment
Know the answer?
Add Answer to:
In Java, Write a program without using a build in square-root operator to compute square-root of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java program 5. Write a short Java program to compute factorial of a given integer from...

    Java program 5. Write a short Java program to compute factorial of a given integer from input using recursive function.

  • Write three functions that compute the square root of an argument using three different methods. The...

    Write three functions that compute the square root of an argument using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number is the values such that x . For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that is in the range 0.0 to 100.0 You program should have a main() that asks the user for x and an accuracy...

  • Java Program: Write an algorithm (steps) using pseudocode (no java code required) explaining steps for a...

    Java Program: Write an algorithm (steps) using pseudocode (no java code required) explaining steps for a method which will reverse an integer array (Integer[]) without using any other data structure. The method should return the reversed integer array. E.g. If input array Integer[] integers = [1,2,3,4,5,6] should return [6,5,4,3,2,1]

  • IN JAVA 1. Create a program that prompts the user for an integer and then displays...

    IN JAVA 1. Create a program that prompts the user for an integer and then displays a message indicating whether or not the number is a perfect square. This can be determined by finding the square root of a number, truncating it (by casting the double result), and then squaring that result 2. Write a program that prompts the user for two numbers. The first number is a minimum value and the second number is a maximum value. RandomNum then...

  • JAVA - Without using build in functions Implement a program that will use a stack structure...

    JAVA - Without using build in functions Implement a program that will use a stack structure to check for correct placement of parentheses in an algebraic expression. Allow the use of ( ) [ ] { } characters as grouping symbols. Make sure that an error is reported for an expression of a form (...]. In addition report other possible parentheses related errors (too many levels, too many right paren., too many left paren.). Make sure to use 'silent error...

  • Java Program Create a Project called “SquareRoot” with a class called “SquareRoot”. Write a Java program...

    Java Program Create a Project called “SquareRoot” with a class called “SquareRoot”. Write a Java program to compute the square root of a double using the algorithm The ancient Babylonians had an algorithm for determining the square root of a number a. Start with an initial guess of a / 2. Then find the average of your guess g and a / g. That’s your next guess. Repeat until two consecutive guesses are close enough. For example, Square Root by...

  • Java ((without using array)) 3. Write a program that reads a 7-digit number and then shuffles...

    Java ((without using array)) 3. Write a program that reads a 7-digit number and then shuffles its digits randon Sample run: Please enter a number 123456 Wrong too small! Sample run: Please enter a number: 1234567 The shuffled number is: 4356271 Sample run: Please enter a number 1000001 The shuffled number is: 0100100

  • Please solve in java Write a program that serves as a simple calculator. The program should...

    Please solve in java Write a program that serves as a simple calculator. The program should ask the user for a number, an operator, and then a second number. The operator should be either '+', '-', 'x', or '/'. The program should then report the answer to the problem. Use a switch statement, not an if statement. You'll need to read the operator as a String and compare it using Strings, as you did in 4.1. Turn in your source...

  • Please create a Java Program, Using Netbeans JDK, that Asks the operator for 2 numbers, and...

    Please create a Java Program, Using Netbeans JDK, that Asks the operator for 2 numbers, and the user's First and Last Name, and calculates the sum, difference, quotient, and product of the 2 numbers. The program should display to screen: "Hello ";{First Name Last Name} "The Sum is ": {Sum} "The Difference is ": {Difference} "The Product is: ": {Product} "The Quotient is ": {Quotient} Each student should post a Java program should have: (a) Pseudocode and (b) adjacent Java...

  • Can someone please help me with how to write this in Java? You will write a...

    Can someone please help me with how to write this in Java? You will write a program to evaluate a math expression given by a user. • Read the input from the scanner • Expressions should match the forms below. Note that you will need spaces between the numbers and operators for your scanner methods to work best. - “number1 operator number2” - “operator number1” • Declare number1 and number2 as type integer. Test using integer values. • You must...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT