File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ #include "main.h"
2+
3+ int find_sqrt (int num , int root );
4+ int _sqrt_recursion (int n );
5+
6+ /**
7+ * find_sqrt - Finds the natural square root of an inputted number.
8+ * @num: The number to find the square root of.
9+ * @root: The root to be tested.
10+ *
11+ * Return: If the number has a natural square root - the square root.
12+ * If the number does not have a natural square root - -1.
13+ */
14+ int find_sqrt (int num , int root )
15+ {
16+ if ((root * root ) == num )
17+ return (root );
18+
19+ if (root == num / 2 )
20+ return (-1 );
21+
22+ return (find_sqrt (num , root + 1 ));
23+ }
24+
25+ /**
26+ * _sqrt_recursion - Returns the natural square root of a number.
27+ * @n: The number to return the square root of.
28+ *
29+ * Return: If n has a natural square root - the natural square root of n.
30+ * If n does not have a natural square root - -1.
31+ */
32+ int _sqrt_recursion (int n )
33+ {
34+ int root = 0 ;
35+
36+ if (n < 0 )
37+ return (-1 );
38+
39+ if (n == 1 )
40+ return (1 );
41+
42+ return (find_sqrt (n , root ));
43+ }
You can’t perform that action at this time.
0 commit comments