Write a Java Program to Find the Square root of a Number using Math.sqrt and without using the sqrt function with example.
Java Program to Find Square root of a Number Example
This program allows the user to enter integer values and then find the square root of that number using the math function Math.sqrt.
import java.util.Scanner; public class SqrtofNumber1 { private static Scanner sc; public static void main(String[] args) { double number, squareRoot; sc = new Scanner(System.in); System.out.print(" Please Enter any Number : "); number = sc.nextDouble(); squareRoot = Math.sqrt(number); System.out.println("\n The Square of a Given Number " + number + " = " + squareRoot); } }
Find Square root of a Number Example 2
In this Program, we are using the math function Math.pow to find the square root of a number. As we all know √number = number½
import java.util.Scanner; public class SqrtofNumber2 { private static Scanner sc; public static void main(String[] args) { double number, squareRoot; sc = new Scanner(System.in); System.out.print(" Please Enter any Number : "); number = sc.nextDouble(); squareRoot = Math.pow(number, 0.5); System.out.println("\n The Square of a Given Number " + number + " = " + squareRoot); } }
Please Enter any Number : 625
The Square of a Given Number 625.0 = 25.0