Compute the positive integer and square root, Computer Engineering


Q1) Write a code that asks the user for a positive integer, computes the square root of that integer, and return the result to the user. The computational error needs to be smaller than 0.01.

1.      Get a positive integer 'X' as the input.

2.      Start from an initial guess, G.

3.      Compute the error = (G^2 - X).

4.      If error < 0.01, report the G as the result and stop. If not, go to 5.

5.      Update G, G = (G + X/G)/2. Go to 3.

Code Block

//This program computes the square root of a given positive integer and is written by Vahid D on 13/1/13

   using namespace std;
   int main()
      double G = 10, err; // G is an initial guess and err is the error
      int X; // input, positive integer
      cout << "Enter a positive integer: \n"; //ask user to enter a number
      cin >> X; // get the input
      err = (G*G - X); // compute the error
      while ( err > 0.01) // continue updating G till the error is smaller than 0.01
          G   = (G + X/G)/2;
          err = (G*G - X);
      cout << "Square root of " << X << " is: " << G << endl; // report the result
      return 0;

Posted Date: 2/15/2013 7:31:32 AM | Location : United States

Related Discussions:- Compute the positive integer and square root, Assignment Help, Ask Question on Compute the positive integer and square root, Get Answer, Expert's Help, Compute the positive integer and square root Discussions

Write discussion on Compute the positive integer and square root
Your posts are moderated
Related Questions
Often calculating all the data is not possible by aggregations for this reason some of the difficult data problems are solved. In order to verify which data should be solved and me

Q. What do you mean by Segment numbers? There is a good reason for not leaving determination of segment numbers up to assembler. It permits programs written in 8086 assembly

Learning algorithm for multi-layered networks: Furthermore details we see that  if S is too high, the contribution from w i * x i is reduced. It means that t(E) - o(E) is mu

What is "at exit-command:? The flow logic Keyword at EXIT-COMMAND is a special addition to the MODULE statement in the Flow Logic .AT EXIT-COMMAND lets you call a module befor

A NULL pointer is a pointer of any type whose value is zero. A void pointer is a pointer to an object of an unknown type, and is guaranteed to have sufficient bits to hold a pointe

Comparison between Motorola processors and INTEL processors: Intel/AMD processors are really about the same thing.  They run the same software and operate in a very similar ma

Objects, messages, class, inheritance and polymorphism are the major concepts of object orientation.

How does the Xml Serializer work?  What ACL permissions does a process using it require?   Xml Serializer needs write permission to the system's TEMP directory.

For a non blocking crossbar configuration, taking N as the number of subscribers, there will be               number of cross points and _             number of switches for est

an asyncronous sequential circuit is described by the the exitatio function and the output function y=x1x2''+(x1=x2'')y output function z=y a)draw the logic diagram of the circuit.