Skip to content

Commit db26f10

Browse files
committed
task 5
1 parent 678d944 commit db26f10

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

0x08-recursion/5-sqrt_recursion.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)