Question

Develop an algorithm and write a code that prompts the user to input a number and...

Develop an algorithm and write a code that prompts the user to input a number and returns the square root of that number.

Homework Answers

Answer #1

Algorithm square root the number

  1. Start with the start = 0 & end = x
  2. while (start <= end)
  3. Compute md As in square root (start + end)/2.
  4. compare the md*md with the x.
  5. If x =md*md.
  6. return md.
  7. Case
  8. If x > md+1 and end.
  9. case
  10. If x< between start and md
  • implementation of this algorithm for the square root of the number

Program-

  • #include<bits.h>
  • using namespace std;
  • int fSqrt(int x)  
  • {   
  • if (x == 0 || x == 1)   
  • return x;
  • int start = 1, end = x, ans;
  • while (start <= end)   
  • {
  • int md = (start + end) / 2;
  • if (md*md == x)
  • return md;
  • if (md*md < x)
  • {
  • start = md + 1;
  • ans = md;
  • }
  • else
  • end = md-1;
  • }
  • return ans;
  • }
  • int main()
  • {
  • int x = 11;
  • cout << fSqrt(x) << endl;
  • return 0;
  • }

@thank you

Know the answer?
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
Similar Questions
Develop an algorithm in Python and write a code that prompts the user to input a...
Develop an algorithm in Python and write a code that prompts the user to input a number and returns the square root of that number.
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
Write and test code in MIPS assembly language program to implement algorithms "The Non-Restoring Algorithm "of...
Write and test code in MIPS assembly language program to implement algorithms "The Non-Restoring Algorithm "of an 8-bit integer the user shoud insert the number then by the algo well find the square root
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
Write a complete Java program which prompts the user of the program to input his/her first...
Write a complete Java program which prompts the user of the program to input his/her first name, then prompts the user for the middle initial and finally prompts the user for the last name. As indicated, there are three prompts to the user. The program should output the user’s name with the first name first, followed by the middle initial, and the last name last, all on one line (with appropriate spacing). If you could also pinpoint exactly where to...
Write a C program that prompts the user to input as many unsigned(n) as the user...
Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number. For each number the user types, output whether that number is prime or not. You won't need...
By using Python code answer the following parts: A) Write a program to ask the user...
By using Python code answer the following parts: A) Write a program to ask the user to input their name and then output the type and length of the input. B) Write a program to output the last 11 letters of 'Introduction to Indiana'. C) Write a program to output the 3rd to the 11th letters of 'Introduction to Indiana'. D) Write a program to ask the user to input a float number and output the rounded value of it...
Q1) Write a program that prompts the user to input an integer that represents cents.The program...
Q1) Write a program that prompts the user to input an integer that represents cents.The program will then calculate the smallest combination of coins that the user has. For example, 27 cents is 1 quarter and 2 pennies. Note : given $bills the change should come has to run from pycharm.
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
Write a java program that repeatedly prompts the user to input a string starting with letters...
Write a java program that repeatedly prompts the user to input a string starting with letters from the English alphabet. The program must stop getting input when the user inputs the string “STOOOOP”. Then the program must display the words starting with each letter from the alphabet in a separate line (e.g. you need to make a new line after all words starting with a specific letter are finished.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT