Babylonian method for square root

Here given code implementation process.

// C program
// babylonian method for square root
#include <stdio.h>

void findSquareRoot(double num)
{
	double a = num;
	double b = 1.0;
	// Here precision (0.000001)
	while ((a - b) > 0.000001)
	{
		a = (a + b) / 2.0;
		b = num / a;
	}
	// Display given number
	printf("\n Given Number : %lf", num);
	// Display the calculate square root
	printf("\n Square Root  : %lf\n", a);
}
int main(int argc, char
	const *argv[])
{
	// Test
	findSquareRoot(64);
	findSquareRoot(10.3);
	findSquareRoot(17.50);
	return 0;
}

Output

 Given Number : 64.000000
 Square Root  : 8.000000

 Given Number : 10.300000
 Square Root  : 3.209361

 Given Number : 17.500000
 Square Root  : 4.183300
// Java program
// Babylonian method for square root
public class SquareRoot
{
	public void findSquareRoot(double num)
	{
		double a = num;
		double b = 1.0;
		// Here precision (0.000001)
		while ((a - b) > 0.000001)
		{
			a = (a + b) / 2.0;
			b = num / a;
		}
		// Display given number
		System.out.print("\n Given Number : " + num);
		// Display the calculate square root
		System.out.print("\n Square Root : " + a + "\n");
	}
	public static void main(String[] args)
	{
		SquareRoot task = new SquareRoot();
		// Test
		task.findSquareRoot(64);
		task.findSquareRoot(10.3);
		task.findSquareRoot(17.50);
	}
}

Output

 Given Number : 64.0
 Square Root : 8.00000000000017

 Given Number : 10.3
 Square Root : 3.209361314240489

 Given Number : 17.5
 Square Root : 4.183300132670613
// Include header file
#include <iostream>
using namespace std;

// C++ program
// Babylonian method for square root

class SquareRoot
{
	public: void findSquareRoot(double num)
	{
		double a = num;
		double b = 1.0;
		// Here precision (0.000001)
		while ((a - b) > 0.000001)
		{
			a = (a + b) / 2.0;
			b = num / a;
		}
		// Display given number
		cout << "\n Given Number : " << num;
		// Display the calculate square root
		cout << "\n Square Root : " << a << "\n";
	}
};
int main()
{
	SquareRoot task = SquareRoot();
	// Test
	task.findSquareRoot(64);
	task.findSquareRoot(10.3);
	task.findSquareRoot(17.50);
	return 0;
}

Output

 Given Number : 64
 Square Root : 8

 Given Number : 10.3
 Square Root : 3.20936

 Given Number : 17.5
 Square Root : 4.1833
// Include namespace system
using System;
// C# program
// Babylonian method for square root
public class SquareRoot
{
	public void findSquareRoot(double num)
	{
		double a = num;
		double b = 1.0;
		// Here precision (0.000001)
		while ((a - b) > 0.000001)
		{
			a = (a + b) / 2.0;
			b = num / a;
		}
		// Display given number
		Console.Write("\n Given Number : " + num);
		// Display the calculate square root
		Console.Write("\n Square Root : " + a + "\n");
	}
	public static void Main(String[] args)
	{
		SquareRoot task = new SquareRoot();
		// Test
		task.findSquareRoot(64);
		task.findSquareRoot(10.3);
		task.findSquareRoot(17.50);
	}
}

Output

 Given Number : 64
 Square Root : 8.00000000000017

 Given Number : 10.3
 Square Root : 3.20936131424049

 Given Number : 17.5
 Square Root : 4.18330013267061
<?php
// Php program
// Babylonian method for square root
class SquareRoot
{
	public	function findSquareRoot($num)
	{
		$a = $num;
		$b = 1.0;
		// Here precision (0.000001)
		while (($a - $b) > 0.000001)
		{
			$a = (($a + $b) / 2.0);
			$b = ($num / $a);
		}
		// Display given number
		echo "\n Given Number : ". $num;
		// Display the calculate square root
		echo "\n Square Root : ". $a ."\n";
	}
}

function main()
{
	$task = new SquareRoot();
	$task->findSquareRoot(64);
	$task->findSquareRoot(10.3);
	$task->findSquareRoot(17.50);
}
main();

Output

 Given Number : 64
 Square Root : 8.0000000000002

 Given Number : 10.3
 Square Root : 3.2093613142405

 Given Number : 17.5
 Square Root : 4.1833001326706
// Node Js program
// Babylonian method for square root
class SquareRoot
{
	findSquareRoot(num)
	{
		var a = num;
		var b = 1.0;
		// Here precision (0.000001)
		while ((a - b) > 0.000001)
		{
			a = ((a + b) / 2.0);
			b = (num / a);
		}
		// Display given number
		process.stdout.write("\n Given Number : " + num);
		// Display the calculate square root
		process.stdout.write("\n Square Root : " + a + "\n");
	}
}

function main()
{
	var task = new SquareRoot();
	// Test
	task.findSquareRoot(64);
	task.findSquareRoot(10.3);
	task.findSquareRoot(17.50);
}
main();

Output

 Given Number : 64
 Square Root : 8.00000000000017

 Given Number : 10.3
 Square Root : 3.209361314240489

 Given Number : 17.5
 Square Root : 4.183300132670613
#  Python 3 program
#  Babylonian method for square root
class SquareRoot :
	def findSquareRoot(self, num) :
		a = num
		b = 1.0
		#  Here precision (0.000001)
		while ((a - b) > 0.000001) :
			a = ((a + b) / 2.0)
			b = (num / a)
		
		#  Display given number
		print("\n Given Number : ", num, end = "")
		#  Display the calculate square root
		print("\n Square Root : ", a )
	

def main() :
	task = SquareRoot()
	#  Test
	task.findSquareRoot(64)
	task.findSquareRoot(10.3)
	task.findSquareRoot(17.50)

if __name__ == "__main__": main()

Output

 Given Number :  64
 Square Root :  8.00000000000017

 Given Number :  10.3
 Square Root :  3.209361314240489

 Given Number :  17.5
 Square Root :  4.183300132670613
#  Ruby program
#  Babylonian method for square root
class SquareRoot 
	def findSquareRoot(num) 
		a = num
		b = 1.0
		#  Here precision (0.000001)
		while ((a - b) > 0.000001) 
			a = (a + b) / 2.0
			b = num / a
		end

		#  Display given number
		print("\n Given Number : ", num)
		#  Display the calculate square root
		print("\n Square Root : ", a ,"\n")
	end

end

def main() 
	task = SquareRoot.new()
	#  Test
	task.findSquareRoot(64)
	task.findSquareRoot(10.3)
	task.findSquareRoot(17.50)
end

main()

Output

 Given Number : 64
 Square Root : 8.00000000000017

 Given Number : 10.3
 Square Root : 3.209361314240489

 Given Number : 17.5
 Square Root : 4.183300132670613
// Scala program
// Babylonian method for square root
class SquareRoot
{
	def findSquareRoot(num: Double): Unit = {
		var a: Double = num;
		var b: Double = 1.0;
		// Here precision (0.000001)
		while ((a - b) > 0.000001)
		{
			a = ((a + b) / 2.0);
			b = (num / a);
		}
		// Display given number
		print("\n Given Number : " + num);
		// Display the calculate square root
		print("\n Square Root : " + a + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SquareRoot = new SquareRoot();
		// Test
		task.findSquareRoot(64);
		task.findSquareRoot(10.3);
		task.findSquareRoot(17.50);
	}
}

Output

 Given Number : 64.0
 Square Root : 8.00000000000017

 Given Number : 10.3
 Square Root : 3.209361314240489

 Given Number : 17.5
 Square Root : 4.183300132670613
// Swift 4 program
// Babylonian method for square root
class SquareRoot
{
	func findSquareRoot(_ num: Double)
	{
		var a: Double = num;
		var b: Double = 1.0;
		// Here precision (0.000001)
		while ((a - b) > 0.000001)
		{
			a = (a + b) / 2.0;
			b = num / a;
		}
		// Display given number
		print("\n Given Number : ", num, terminator: "");
		// Display the calculate square root
		print("\n Square Root : ", a );
	}
}
func main()
{
	let task: SquareRoot = SquareRoot();
	// Test
	task.findSquareRoot(64);
	task.findSquareRoot(10.3);
	task.findSquareRoot(17.50);
}
main();

Output

 Given Number :  64.0
 Square Root :  8.00000000000017

 Given Number :  10.3
 Square Root :  3.20936131424049

 Given Number :  17.5
 Square Root :  4.18330013267061
// Kotlin program
// Babylonian method for square root
class SquareRoot
{
	fun findSquareRoot(num: Double): Unit
	{
		var a: Double = num;
		var b: Double = 1.0;
		// Here precision (0.000001)
		while ((a - b) > 0.000001)
		{
			a = (a + b) / 2.0;
			b = num / a;
		}
		// Display given number
		print("\n Given Number : " + num);
		// Display the calculate square root
		print("\n Square Root : " + a + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: SquareRoot = SquareRoot();
	// Test
	task.findSquareRoot(64.0);
	task.findSquareRoot(10.3);
	task.findSquareRoot(17.50);
}

Output

 Given Number : 64.0
 Square Root : 8.00000000000017

 Given Number : 10.3
 Square Root : 3.209361314240489

 Given Number : 17.5
 Square Root : 4.183300132670613


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved