38.6k views
4 votes
For java

Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print:

7 9 11 10 10 11 9 7

Hint: Use two for loops. Second loop starts with i = NUM_VALS - 1.

User Ergosys
by
5.0k points

1 Answer

2 votes

Answer:

public class array{

public static void main(String []args){

int[] array = {2,4,7,1,9};

int num_vals = array.length;

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

System.out.println(array[i] + " ");

}

for(int i=num_vals-1;i>=0;i--){

System.out.println(array[i] + " ");

}

}

}

Step-by-step explanation:

First create the class in the java programming.

Then create the main function and declare the array with values.

Store the size of array in num_vals variable by using the function array.length.

create a for loop to iterate the each element in the array and then print on the screen with spaces and newline.

it traverse the loop from first to last.

Then, again create the for loop to iterate the each element in the array and then print on the screen with spaces and newline but the traversing start from last to first.

User Charles Wu
by
4.6k points