How to find square root in java – Java Program to Find Square and Square root of a Number

How to find square root in java: In the previous article, we have seen Java Program to Find Logarithm of a Number

In this article we are going to see how to find square and square root of a number using java programming language.

Java Program to Find Square and Square root of a Number

How to square root a number in java: Before jumping into the program directly let’s know what is square and square root of a number.

Square: When we multiply the number with itself then we get the square value.

For example:

  • If number is 10 then square of 10 is 100.
  • If number is 5 then square of 5 is 25.
  • If number is 16 then square of 16 is 256.

Square root: It is the number which is multiplied with itself to find the actual number.

For example:

  • If number is 100 then square root of 100 is 10.
  • If number is 5 then square root of 5 is 2.236(approx.)
  • If number is 16 then square root of 16 is 4.

Let’s see different ways to find square and square root of a number.

Method-1: Java Program to Find Square By Multiplying With Itself and Square root By Using Math.sqrt() Method

Approach:

  1. Create scanner class object.
  2. Take user input for the number.
  3. To find the square multiply the number with itself.
  4. To find square root use Math.sqrt() method

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        // find the square of a number
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number to find square: ");
        double number = sc.nextDouble();
        double square = number * number;
        System.out.println("The square of " + number + " is " + square);
        // find the square root of a number
        System.out.print("Enter a number to find square root: ");
        number = sc.nextDouble();
        square = Math.sqrt(number);
        System.out.println("The square root of " + number + " is " + square);
    }
}

Method-2: Java Program to Find Square By Multiplying With Itself and Square root By Using Babylonian Method

Approach:

  1. Create scanner class object.
  2. Take user input for the number.
  3. To find the square multiply the number with itself.
  4. To find square root use Babylonian method.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // find the square of a number
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number to find square: ");
        double number = sc.nextDouble();
        double square = number * number;
        System.out.println("The square of " + number + " is " + square);
        // find the square root of a number
        System.out.print("Enter a number to find square root: ");
        number = sc.nextDouble();
        //finding square root by calling square_Root() user defined method
        System.out.println("The square root of " + number + " is " + square_Root(number));

    }
    
    //method to find square root
    public static double square_Root(double num) 
    {
        double a = num;
        double b = 1;
        double e = 0.000001;
        while (a - b > e) 
        {
            a = (a + b) / 2;
            b = num / a;
        }
        return a;
    }

}
Output:

Enter a number to find square: 5
The square of 5.0 is 25.0
Enter a number to find square root: 16
The square root of 16.0 is 4.000000000000051

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: