Write a C program square root using Babylonian method. Babylonian method of finding square root is one of the oldest method. It uses divide and average technique. It is an iterative method which involves below steps:

C program square root using Babylonian method

1)Guess a positive number
 
2)Divide the original number by the guess

    y = number/x 

3) Find the average of the x and y.

   x = (x + y)/2 
4) Now this average value (x) becomes new “guess” and go back to step 2

This will continue until condition if (x - number/x) >erorr,where error =0.0000001

Here is sample C program square root using Babylonian method:

#include<stdio.h>

double squareRoot(double num) {
	double error = 0.0000001;
	double x = num;

	while ((x - num / x) > error) {

		x = (x + num / x) / 2;
	}
	return x;

}

int main() {

	double num = 0;
	printf("Enter number for finding square root:");
	scanf("%lf", &num);
	printf("Square root of %lf is %lf\n", num, squareRoot(num));
	return 0;
}

 



Related Contents to follow