In this article we will show you, How to write a Java Program to Find Square root of a Number using Math.sqrt, and without using sqrt function with example.
Java Program to Find Square root of a Number Example 1
This program allows the user to enter integer value, and then finds square root of that number using math function Math.sqrt
// Java Program to Find Square root of a Number 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); } }
OUTPUT
Java Program to 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½
// Java Program to Find Square root of a 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); } }
OUTPUT
Thank You for Visiting Our Blog