I have an integer array like this
int[] arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
Now I want to change every multiple of 3 (but not multiple of 5) to 'AB', every multiple of 5 ((but not multiple of 3) to 'CD' & every every multiple of both 3 & 5 to 'ABCD
I want a dynamic solution which can be used for any number of elements of array - that the solution should stand irrespective of size.
I was trying like this
for (int i = 0; i <= n; i++) {
if ((i % 3 == 0) || (i % 5 != 0)) {
arr[i] = 'AB';
}
else if ((i % 3 != 0) || (i % 5 == 0)) {
arr[i] = 'CD';
}
else if ((i % 3 == 0) || (i % 5 == 0)) {
arr[i] = 'ABCD';
}
}
But it is giving error in Eclipse as i am unable to assign string value in integer array. I am beginner in Java, that is why I am requesting someone to help me in this regard.
The final array will be [1, 2, AB, 4, CD, AB, 7, 8, AB, CD, 11, AB, 13, 14, ABCD, 16, 17, AB, 19, CD];