Java Tutorial
In java programming, this program is used to compute the square root value for double number provided as user input.
Suppose user gives the 4.0, then this program computes the square root value 2.0.
Scanner nextDouble method is used to read the double number as user input.
If user provides a invalid value for number then InputMismatchException is raised.
import java.util.Scanner; import java.util.InputMismatchException; import java.lang.Math; public class SquareRoot{ public static void main(String[] args){ System.out.print("Enter Number: "); Scanner sc=new Scanner(System.in); double number = 0; try { number = sc.nextDouble(); } catch(InputMismatchException ime){ System.out.println("Invalid input double number"); return; } double squareRootValue = Math.sqrt(number); System.out.println("Square Root of '"+number+"': "+squareRootValue); } }Output:
D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: 4 Square Root of '4.0': 2.0 D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: 25.50 Square Root of '25.5': 5.049752469181039 D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: a Invalid input double number
In java programming, this program is used to compute the square root value for integer number provided as user input.
Scanner nextInt method is used to read the integer number as user input.
import java.util.Scanner; import java.lang.Math; import java.util.InputMismatchException; public class SquareRoot{ public static void main(String[] args){ System.out.print("Enter Number: "); Scanner sc=new Scanner(System.in); int number = 0; try { number = sc.nextInt(); } catch(InputMismatchException ime){ System.out.println("Invalid input integer number"); return; } double squareRootValue = (int)Math.sqrt((double)number); System.out.println("Square Root of '"+number+"': "+squareRootValue); } }Output:
D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: 4 Square Root of '4': 2.0 D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: a Invalid input integer number D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: 25.50 Invalid input integer number
In java programming, this program is used to compute the square root value for float number provided as user input.
Scanner nextFloat method is used to read the float number as user input.
import java.util.Scanner; import java.lang.Math; import java.util.InputMismatchException; public class SquareRoot{ public static void main(String[] args){ System.out.print("Enter Number: "); Scanner sc=new Scanner(System.in); float number = 0; try { number = sc.nextFloat(); } catch(InputMismatchException ime){ System.out.println("Invalid input integer number"); return; } float squareRootValue = (float)Math.sqrt((double)number); System.out.println("Square Root of '"+number+"': "+squareRootValue); } }Output:
D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: 4 Square Root of '4': 2.0 D:\Java_Programs>javac SquareRoot.java D:\Java_Programs>java SquareRoot Enter Number: a Invalid input integer number Enter Number: 25.50 Square Root of '25.5': 5.049752
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page