# Replace the Array Elements with -6 if the Last Digit is 6 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 an array with some random values and we have to find if any elements have the last digit as 6 or the element itself is equal to 6 then we have to replace that element with -6.

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

## To show you some instances

### Instance-1

Given Array= [21, 16, 18, 5, 6].
Replace the digit 6 from the given array with -6.
New array = [21, -6, 18, 5, -6]


### Instance-2

Given Array= [38, 94, 86, 63, 36].
Replace the digit 6 from the given array with -6.
New array = [38, 94, -6, 63, -6]


### Instance-3

Given Array= [54, 67, 26, 95, 24, 60].
Replace the digit 6 from the given array with -6.
New array = [54, 67, -6, 95, 24, 60]


## Algorithm

• Step 1 − Declare and initialize an array.

• Step 2 − Declare another empty array with same length as of given array.

• Step 3 − Take a for loop to check every element of the given array and check whether the last digit is 6 or not.

• Step 4 − If there is any element present which ends with digit 6 then replace that element with -6.

• Step 5 − After getting result array, print that array as output.

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

## Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Array Initialization.

• By User-Defined Method.

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

## Approach-1: By Using Static Array Initialization

In this approach, we declare and initialize an array and by using the algorithm we can replace the array elements if the last digit of any element is 6 then replace the element with -6.

### Example

import java.util.*;
public class Main {
public static void main(String args[]) {

// declare an integer type of array and store some random value to it by static input method
int[] inputArray = { 264, 926, 6, 52 , 21,54};

//declare an empty array to store the modified array
int[] newArray = new int[inputArray.length];

//initiate the loop to replace the digit-6 with "-6"
for(int i=0,j=0;i<inputArray.length;i++) {
if(inputArray[i]%10==6) {
newArray[i]=-6;
} else {
newArray[i]=inputArray[i];
}
}
//print the resultant array
System.out.println("Resultant array: " + Arrays.toString(newArray));
}
}


### Output

Resultant array: [264, -6, -6, 52, 21, 54]


## Approach-2: By Using User Defined Method

In this approach, we declare and initialize an array. Then call a user defined method by passing this array and the given number as parameter in user defined method, then inside the method by using the algorithm we can replace the array elements if the last digit of any element is 6 then replace the element with -6.

### Example

import java.util.*;
public class Main {
public static void main(String args[]) {

// declare an integer type of array and store some random value to it by static input method
int[] inputArray = { 26, 92, 61, 56 , 6};
changeDigit(inputArray);
}
public static void changeDigit(int[] arr) {
int[] newArray = new int[arr.length];
for(int i=0,j=0;i<arr.length;i++){
if(arr[i]%10==6){
newArray[i]=-6;
} else {
newArray[i]=arr[i];
}
}
System.out.println("Resultant array: " + Arrays.toString(newArray));
}
}


### Output

Resultant array: [-6, 92, 61, -6, -6]


In this article, we explored how to replace the array elements with -6 if the last digit of any element is 6 by using Java programming language.

Updated on: 02-Feb-2023

128 Views 