Try this exercise to practice everything so far. Write a program that:
Creates an int array of length
Fills each slot with the square of its index (so index gets , index gets , index gets , and so on)
Prints every element on its own line
Here is one solution:
int[] squares = new int[7];
for (int i = 0; i < squares.length; i++) {
squares[i] = i * i;
}
for (int i = 0; i < squares.length; i++) {
System.out.println(squares[i]);
}
Notice the loop condition uses i < squares.length rather than a hardcoded number. This pattern keeps your code correct even if you change the array's size later.