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
When we initialize the form at that time if we require to set any variable's value then we can set it by defining in constructor. Constructor's name always similar as class name an

Illustrate about Sharing of Structure and Behaviour  One of the reasons for the popularity of object-oriented techniques is that they promote sharing at different levels. Inher

Build the circuit using the Asynchronous Counter Technique with JK FF and relevantgates capable of executing the counting sequence as {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}Simulate the cir

Many medium-to-large information services units for modern business have reorganized to be decentralized with an emphasis on dynamic teams andempowerment. In modern business system

Problem: (a) What shows a Pattern a Pattern? (b) Which pattern is given below? Justify your answer. public class A { private static A instance = null; private A() {

Explain Performance Evaluation in parallel Computing In this section, we will discuss the key attributes used to measure performance of a computer system. The performance chara

Q. Starting parallel virtual machine? To initialize PVM on any host on that PVM has been installed we can type  % pvm The PVM console known as pvm is a standalone PVM t

The difference among the next and continue verb is that in the continue verb it is used for a situation where there is no EOF condition that is the records are to be accessed again

Define the concept of Typing of object oriented analysis Typing enforces object class such that objects of different classes cannot be interchanged.  Or we can say that, class