Compute the positive integer and square root, Computer Engineering

Question:

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

#include
   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
Q. Signaling PVM process? int pvm_sendsig( int tid, int signum ) Transmits a signal to other PVM process. tid is task identifier of PVM process to receive signal.

Advantages to companies adopting call centres Computer systems have allowed companies to set up call centres in other nations where potential cost savings can be made. This, of

how create database design for pharmacy by diagram and query

What are the types of data warehouses? Various types of data warehouses are illustrated below: a) Financial Data Warehouses b) Insurance Data Warehouses c) Human Resou


CIH, also known  as Chernobyl or Spacefiller, is a Microsoft Windows computer virus which first emerged in 1998. It is one of the most damaging viruses, overwriting critical inform

Program to calculate the value of the series 1+x+x2+x3+.... +xn. Read the values of ''x'' and ''n'' from the keyboard.

write alp to perform bcd addition without using procedure

The First In First Out method assumes items of stocks are issued in the same order as they are received. Thus the goods received first are assumed to be the first to be issued .Any

expplain in detail the internal organisation of computer?