Tuesday, 21 August 2018

Java program to find out square root of a given number


Many mathematical operations have an inverse, or opposite, operation. Subtraction is the opposite of addition, division is the inverse of multiplication, and so on. Squaring, which we learned about in a previous lesson (exponents), has an inverse too, called "finding the square root." Remember, the square of a number is that number times itself. The perfect squares are the squares of the whole numbers: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 …
The square root of a number, n, written

 is the number that gives n when multiplied by itself. For example,

because 10 x 10 = 100

Examples

Here are the square roots of all the perfect squares from 1 to 100.
Java program to find out square root of a given number
Java Program
/*Java program to find out square root of a given number
 */
public class SquareRootDemo
{
    public static void main(String[] args)
    {
        double number = 16;
        double squareRoot = Math.sqrt(number);

        //Displaying the values
        System.out.println("number : "+number);
        System.out.println("Square Root : "+squareRoot);
    }

}

public class SquareRoot {
   
    public static double calculateSqrt (int n) {
       
    double rootValue = 1.00;
    double num = n;
   
    for (int i = 0; i < n; i++) {
       
        rootValue = 0.5 * (rootValue + num/rootValue);
    } 
   
    //To make only 3 digit available after decimal point
    int returnValue = (int)(rootValue * 1000); 
    rootValue = returnValue;
    rootValue /=1000;
   
    System.out.println("Root Value Calculated : " + rootValue);
    System.out.println("Root Value By InBuild Method Method : " + Math.sqrt(n));
   
    return rootValue;
   
    }
   
   
    public static void main (String[] args) {
       
        int n;
       
        Scanner input = new Scanner (System.in);       
       
        System.out.println("Enter Number to find its square root-");
        n = input.nextInt();   
       
        calculateSqrt(n);
    }

}


Java Program to Find Square root of a Number Example
// 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);
}
}

Java Program to Find Square root of a Number Example
// 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);
}
}

0 comments:

Post a Comment