Replace Odd Numbers with Square root & Even Numbers with Square in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to replace odd numbers by its square root and all even numbers by its square in the given array. A number is said to be an even number if it is divisible by 2 else it is called an odd number.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {49, 9, 12, 6, 64, 81, 22}.
After finding odd numbers by its square root and all even numbers by its square:
Original array is: [7, 3, 144, 36, 4096, 9, 484]
Even numbers with their square in the given array are:
Even Number | Square
12 -- 144
6 -- 36
64 -- 4096
22 -- 484
Odd numbers with their square root in the given array are:
Odd Number | Square root
49 -- 7
9 -- 3
81 -- 9

Instance-2

Suppose the original array is {51, 19, 22, 61, 4, 8, 32}
After finding odd numbers by its square root and all even numbers by its square:
Original array is: [7, 4, 484, 7, 16, 64, 1024]
(Here we have typecast double value to int value)
Even numbers with their square in the given array are:
Even Number | Square
22 -- 484.0
4 -- 16.0
8 -- 64.0
32 -- 1024.0
Odd numbers with their square root in the given array are:
Odd Number | Square root
51 -- 7.14142842854285
19 -- 4.358898943540674
61 -- 7.810249675906654

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Find the even element by “arr[i]%2 == 0” and it’s square by “Math.pow(arr[i],2)”.

  • Step 3 − Find the odd element by “arr[i]%2 != 0” and it’s square by “Math.sqrt(arr[i])”.

  • Step 4 − Print the elements of the array.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length

Below refers to the syntax of it −

array.length

where, ‘array’ refers to the array reference.

To get the square root of a number we have inbuilt sqrt() method in the Math class of java.lang package.

Following is the syntax to get square root of any number by using the method

double squareRoot = Math.sqrt(input_vale)

Similarly, to get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the method −

double power = Math.pow (inputValue,2)

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array

In this approach, array elements will be initialized in the program. Then as per the algorithm replace odd numbers by its square root and all even numbers by its square of the array.

Example

import java.util.*;
public class Main {
   public static void main(String args[]) {
      
      //Declare and initialize the array elements
      int[] arr = { 49, 9, 12, 6, 64, 81, 22 };
      
      //printing original array
      System.out.println("Original array is: " + Arrays.toString(arr));
      
      //Iterating the array elemnts one by one and replacing
      //Odd numbers with their square root
      
      //Even numbers with their square
      for (int i=0; i<arr.length; i++){
      
         //check for even element
         if(arr[i]%2 == 0) {
         
            //printing even array elements with their square
            arr[i] = (int)Math.pow(arr[i],2);
         } else {
            arr[i] = (int)Math.sqrt(arr[i]);
         }
      }
      System.out.println("Updated array is: "+ Arrays.toString(arr));
   }
}

Output

Original array is: [49, 9, 12, 6, 64, 81, 22]
Updated array is: [7, 3, 144, 36, 4096, 9, 484]

Approach-2: By Using User Defined Method

In this approach, array elements will be initialized in the program. Then call a user-defined method by passing the array as parameter and inside method as per the algorithm t replace odd numbers by its square root and all even numbers by its square of the array.

Example

import java.util.*;
public class Main {
   public static void main(String args[]) {
      
      //Declare and initialize the array elements
      int[] arr = { 49, 9, 12, 6, 64, 81, 22 };
      
      //calling the user defined method
      replace(arr);
   }
   
   //user defined method
   public static void replace(int []arr) {
      //printing original array
      System.out.println("Original array is: " + Arrays.toString(arr));
      
      //Iterating the array elemnts one by one and replacing 
      //Odd numbers with their square root
      
      //Even numbers with their square
      for (int i=0; i<arr.length; i++) {
      
         //check for even element
         if(arr[i]%2 == 0) {
         
            //printing even array elements with their square
            arr[i] = (int)Math.pow(arr[i],2);
         } else {
            arr[i] = (int)Math.sqrt(arr[i]);
         }
      }
      System.out.println("Updated array is: "+ Arrays.toString(arr));
   }
}

Output

Original array is: [49, 9, 12, 6, 64, 81, 22]
Updated array is: [7, 3, 144, 36, 4096, 9, 484]

In this article, we explored different approaches to replace odd numbers by its square root and even numbers by its square in an array by using Java programming language.

Updated on: 02-Feb-2023

490 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements