Hi All,
Here I write programs for finding the Square root of a given number using both manual and Java inbuilt functionality :-)
import java.util.Scanner;
public class squareRoot {
public static void main(String args[]) {
System.out.println("SquareRoot in Java ");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number to find square root: ");
double square = scanner.nextDouble(); //Reading value from console
double squareRoot = Math.sqrt(square); // Java inbuilt functionality for squareRoot the given number
System.out.println("Square root of Given number: " + square + " is -> " + squareRoot);
System.out.println("");
//Manually calculating SquareRoot of given number
System.out.println("Enter number to find square root: ");
double square1 = scanner.nextDouble();
manuallySquare(square1);
}
public static void manuallySquare(double square) {
double x = 1;
for (int i = 0; i < square; i++) {
x = 0.5 * (x + square / x);
}
System.out.println("Square root of Given number: " + square + " is: -> " + x);
}
}
Output:
~~~~~~
SquareRoot in JavaEnter number to find square root:
25
Square root of Given number: 25.0 is -> 5.0
Enter number to find square root:
64
Square root of Given number: 64.0 is: -> 8.0
No comments:
Post a Comment