- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes

- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages

- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation

- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples

# 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.

- Related Articles
- Sum of square of first n odd numbers
- Even numbers at even index and odd numbers at odd index in C++
- How to find Square root of complex numbers in Python?
- How can we find the square root of numbers?
- Find the square root of the given decimal numbers: $9.61$.
- Write the possible unit’s digits of the square root of the following numbers. Which of these numbers are odd square roots?(i) 9801(ii) 99856(iii) 998001(iv) 657666025.
- Java Program to Find Sum of First N Odd numbers and Even numbers
- Find the square root of the following numbers in decimal form:84.8241
- Find the square root of the following numbers in decimal form:0.7225
- Find the square root of the following numbers in decimal form:0.813604
- Find the square root of the following numbers in decimal form:0.00002025
- Find the square root of the following numbers in decimal form:150.0625
- Find the square root of the following numbers in decimal form:225.6004
- Find the square root of the following numbers in decimal form:3600.720036
- Find the square root of the following numbers in decimal form:236.144689