Skip to content

Commit 811fa1c

Browse files
committed
Solution to find square root of a number
1 parent 357ce4f commit 811fa1c

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/Basics/SquareRootOfNumber.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package Basics;
2+
3+
public class SquareRootOfNumber {
4+
public static void main(String[] args) {
5+
6+
int result = findSquareRootOfNumber(11);
7+
System.out.println(result);
8+
}
9+
10+
static int findSquareRootOfNumber(int number){
11+
12+
// Base cases
13+
if (number == 0 || number == 1)
14+
return number;
15+
16+
// Starting from 1, try all numbers until
17+
// i*i is greater than or equal to x.
18+
int i = 1, result = 1;
19+
20+
while (result <= number) {
21+
i++;
22+
result = i * i;
23+
System.out.println(i + " " + result);
24+
}
25+
return i - 1;
26+
27+
}
28+
}

0 commit comments

Comments
 (0)