so I'm doing a small exercise called 'array of multiples', where I am multiplying a number and displaying all the multiples of the number until the answer is reached by an array.
for example: arrayOfMultiples(7, 5) ➞ [7, 14, 21, 28, 35].
My issue is that I have an additional element being added to my array with a value of 0, and I do not understand why. I've tried to understand why but I need help!
this is my code:
public static void main(String[] args) {
//invoke method
int[] test = arrayOfMultiples(7, 6);
//convert array to string to print entire thing
System.out.println(Arrays.toString(test));
}
public static int[] arrayOfMultiples(int num, int length) {
//array is initalised with original number
int [] array = new int [num];
//variable to keep num the same
int add = num;
// loop for adding the new multiples to the array
for (int i = 0; i < length; i++) {
array[i] = num;
num += add;
}
return array;
}
my output is: [7, 14, 21, 28, 35, 42, 0]
thank you!