Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Find the square root of a number using Babylonian method

C++ Math: Exercise-16 with Solution

Write a C++ program to find the square root of a number using Babylonian method.

Sample Input: n = 50
Sample Output: 7.07107

Sample Input: n = 81
Sample Output: 9

Sample Solution:

C++ Code :

#include <iostream>

using namespace std; 

float square_Root(float num) 
    { 
        float x = num; 
        float y = 1; 
        float e = 0.000001;
        while (x - y > e) { 
            x = (x + y) / 2; 
            y = num / x; 
        } 
        return x; 
    } 
  
int main() 
{ 
    int n = 50; 
    cout << "Square root of " << n << " is " << square_Root(n); 
    n = 81; 
    cout << "\nSquare root of " << n << " is " << square_Root(n);     
    return 0;
}

Sample Output:

Square root of 50 is 7.07107
Square root of 81 is 9

Flowchart:

Flowchart: Find the square root of a number using Babylonian method.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C++ programming to find the nth digit of number 1 to n.
Next: Write a C++ program to multiply two integers without using multiplication, division, bitwise operators, and loops.

What is the difficulty level of this exercise?