forked from ShivangiSingh17/Java-Jet
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareRoot.java
More file actions
Latest commit
35 lines (25 loc) · 1 KB
/
Copy pathSquareRoot.java
File metadata and controls
35 lines (25 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
importjava.util.Scanner;
publicclassSquareRoot {
publicstaticvoidmain (Stringargs[]) {
Scannerin = newScanner(System.in);
System.out.println("Please enter a number : ");
intval = in.nextInt();
//loops until user gives a valid number
while (val <= 0) {
System.out.println("Please enter a value greater than 0");
val = in.nextInt();
}
doublevalCopy;
doublesquareRoot = val / 2;
valCopy = squareRoot;
squareRoot = (valCopy + (val / valCopy)) / 2;
// Uses a loop comparing our value to its root. The loop will become false when
// square root next value is our inputted number
while (true) {
if ((valCopy - squareRoot) == 0) break;
valCopy = squareRoot;
squareRoot = (valCopy + (val / valCopy)) / 2;
}
System.out.printf("Square root of %d is %.1f", val, squareRoot);
}
}