How to do a square root in java – Java Program to Find Square Root of Each Element of the Array

How to do a square root in java: In the previous article, we have seen  Java Program to Square Each Element of the Array

In this article we are going to see how to find square root of each element of the array by using Java Language.

Java Program to Find Square Root Each Element of the Array

Squareroot in java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find square root each element of the array.

Method-1: Java Program to Square Each Element of the Array By Using Math.sqrt() Function

Approach:

  • Take the array size as user input.
  • Then take the array elements as user input.
  • Iterate each element of the array and find the square root of each element of the array by using Math.sqrt() inbuilt function.
  • Then print the new array.

Program:

import java.util.*;

public class Main
{

    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        
        // initialize array with size n
        double arr[] = new double[n];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        //iterating the array
        for(int i=0;i<arr.length;i++)
        {
            // finding square root of array element by using inbuilt function sqrt()
            // and replacing the old value with new value
            arr[i] = Math.sqrt(arr[i]);
        }
        
        //printing the result array 
        System.out.println("New array after finding square root of each element : ");
        for(int i=0;i<arr.length;i++)
        {
            System.out.print(arr[i]+" ");
        }    
   }
}
Output:
Enter the size of array: 5
Enter array elements: 49 26 64 16 36
New array after finding square of each element : 
7.0 5.0990195135927845 8.0 4.0 6.0

Method-2: Java Program to Square Each Element of the Array By Using Math.pow() Function

Approach:

  • Take the array size as user input.
  • Then take array elements as user input.
  • Iterate each element of the array and find the square root of array element by using Math.pow() function.
  • Then print the new array.

Program:

import java.util.*;

public class Main
{

    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        
        // initialize array with size n
        double arr[] = new double[n];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        //iterating the array
        for(int i=0;i<arr.length;i++)
        {
            // finding square root of array element by using inbuilt function pow()
            // and replacing the old value with new value
            arr[i] = Math.pow(arr[i],0.5);
        }
        
        //printing the result array 
        System.out.println("New array after finding square root of each element : ");
        for(int i=0;i<arr.length;i++)
        {
            System.out.print(arr[i]+" ");
        }    
   }
}
Output:

Enter the size of array: 6
Enter array elements: 49 36 16 25 33 64
New array after finding square root of each element : 
7.0 6.0 4.0 5.0 5.744562646538029 8.0

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Help yourself by practicing these:

  1. Square root program in java
  2. Square of array elements in java
  3. Java program to find square of a number
  4. Sum of square root program in java
  5. Java program to find out the square roots of the array elements
  6. Java program to find square root of a number without sqrt
  7. Java program to find square root of each element of the array

Related Java Programs: