179k views
4 votes
Write a program that creates an integer array with 40 elements in it. Use a for loop to assign values to each element of the array so that each element has a value that is triple its index. For example, the element with index 0 should have a value of 0, the element with index 1 should have a value of 3, the element with index 2 should have a value of 6, and so on.

User Laker
by
5.0k points

1 Answer

2 votes

Answer:

public class Main

{

public static void main(String[] args) {

int[] numbers = new int[40];

for (int i=0; i<40; i++){

numbers[i] = i * 3;

}

for (int i=0; i<40; i++){

System.out.println(numbers[i]);

}

}

}

Step-by-step explanation:

*The code is in Java.

Initialize an integer array with size 40

Create a for loop that iterates 40 times. Inside the loop, set the number at index i as i*3

i = 0, numbers[0] = 0 * 3 = 0

i = 1, numbers[1] = 1 * 3 = 3

i = 2, numbers[2] = 2 * 3 = 6

.

.

i = 39, numbers[39] = 39 * 3 = 117

Create another for loop that iterates 40 times and prints the values in the numbers array

User Robby Pond
by
5.3k points