In: Computer Science
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
Answer :
Java Code :
public class AddValueToArray {
// Method to add value in the all element of array
public static void addValueTo(int value, int array[]) {
for(int i=0; i< array.length; i++) {
array[i] += value;
}
}
// DO NOT MODIFY main!
public static void main(String[] args) {
int[] array = new int[]{3, 8, 6, 4};
int valueToAdd = Integer.parseInt(args[0]);
addValueTo(valueToAdd, array);
for (int index = 0; index < array.length; index++) {
System.out.println(array[index]);
}
}
}
Output :
C:\Users\UserName\Desktop>java addValueToArray.java 5
8
13
11
9
Hope you like it
Any Query? Comment Down!
I have written for you, Please up vote the answer as it encourage us to serve you Best !