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

  • برمجة سي بلس بلس
  • برمجة

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 Output:

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

الأجوبة

#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;
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...