Root in java – Java Program to Create an Array Containing the Square root of All the Elements in the Array

Root in java: In the previous article, we have seen Java Program to Generate 30 Terms of Fibonacci number in an Array

In this article we are going to see how to create an array containing the square root of all the elements present in the array using Java programming language.

Java Program to Create an Array Containing the Square root of All the Elements in the Array

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 create an array containing the square root of all the elements present in the array.

Method-1: Java Program to Create an Array Containing the Square root of All the Elements in the Array By Using Math.sqrt() function and Static Initialization of Array Elements

Approach:

  1. Declare and initialize and array.
  2. Iterate over the array.
  3. Calculate the square root using Math.sqrt() method.
  4. Print the array.

Program:

import java.util.Arrays;

public class Main
{
    public static void main(String[] args) 
    {
        int[] arr = { 2, 4, 9, 16, 10 };
        double[] sqrt = new double[arr.length];
        
        //Iterating each element of the array
        for (int i = 0; i < arr.length; i++) 
        {
            //find square root of each element using Math.sqrt() function
            sqrt[i] = Math.sqrt(arr[i]);
        }
        System.out.println("Original array: " + Arrays.toString(arr));
        System.out.println("Array after finding the square of the elements: " + Arrays.toString(sqrt));
    }
}
Output:

Original array: [2, 4, 9, 16, 10]
Array after finding the square of the elements: [1.4142135623730951, 2.0, 3.0, 4.0, 3.1622776601683795]

Method-2: Java Program to Create an Array Containing the Square root of All the Elements in the Array By Using Math.sqrt() function and Dynamic Initialization of Array Elements

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Iterate over the array.
  6. Calculate the square root using Math.sqrt() method.
  7. Print the 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
        int[] arr = new int[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();
        }
        // initialize array with size n
        double[] sqrt = new double[arr.length];
        for (int i = 0; i < arr.length; i++) 
        {
            sqrt[i] = Math.sqrt(arr[i]);
        }
        System.out.println("Original array: " + Arrays.toString(arr));
        System.out.println("Array after finding the square of the elements: " + Arrays.toString(sqrt));
    }
}

Output:

Enter the size of array: 5
Enter array elements: 4 6 16 9 25
Original array: [4, 6, 16, 9, 25]
Array after finding the square of the elements: [2.0, 2.449489742783178, 4.0, 3.0, 5.0]

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: