Repository: TheAlgorithms/Java
Pull request #2883 (2021-12-23 19:51:05)

added java code of babylonian method
### **Describe your change:**

- Add an algorithm


#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


github-actions[bot]: This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
SaumyaBhushan: > 

replaced main() with test.
22aam: can you tell me about this pull request 
SaumyaBhushan: > can you tell me about this pull request

Babylonian method is an iterative method to find the square root of a number. This is also sometimes called as Hero's method

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java
new file mode 100644
index 00000000..306bb123
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/SquareRootWithBabylonianMethod.java
@@ -0,0 +1,25 @@
+package com.thealgorithms.maths;
+
+import java.util.Scanner;
+
+
+public class SquareRootWithBabylonianMethod {
+    /**
+     * get the value, return the square root
+     *
+     * @param num contains elements
+     * @return the square root of num
+     */
+    public static float square_Root(float num)
+    {
+        float a = num;
+        float b = 1;
+        double e = 0.000001;
+        while (a - b > e) {
+            a = (a + b) / 2;
+            b = num / a;
+        }
+        return a;
+    }
+
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/SquareRootwithBabylonianMethodTest.java b/src/test/java/com/thealgorithms/maths/SquareRootwithBabylonianMethodTest.java
new file mode 100644
index 00000000..e5a1c27b
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/SquareRootwithBabylonianMethodTest.java
@@ -0,0 +1,26 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class SquareRootwithBabylonianMethodTest {
+    @Test
+    void testfor4(){
+        Assertions.assertEquals(2,SquareRootWithBabylonianMethod.square_Root(4));
+    }
+
+    @Test
+    void testfor1(){
+        Assertions.assertEquals(1,SquareRootWithBabylonianMethod.square_Root(1));
+    }
+
+    @Test
+    void testfor2(){
+        Assertions.assertEquals(1.4142135381698608,SquareRootWithBabylonianMethod.square_Root(2));
+    }
+
+    @Test
+    void testfor625(){
+        Assertions.assertEquals(25,SquareRootWithBabylonianMethod.square_Root(625));
+    }
+}