In: Computer Science
Write a Java program to initialize an array with the even
integers from 2 to 20 and print the result then pass this array to
a method in order to create a new array which is the inverse of the
array you passed (print the result).
You cannot use a third array.
Insert comments and version control in the program to document the
program.
public class Main
{
//method to reverse the array
public static void reverse(int arr[], int size)
{
//variable declaration
int temp;
//reverse the array
for(int i=0; i<size/2; i++)
{
temp = arr[i];
arr[i] = arr[size-i-1];
arr[size-i-1] = temp;
}
}
public static void main(String[] args)
{
//array declaration
int array[] = new int[10];
//variable declaration and
initialization
int i=0;
//fill the array with even
integer
for(int value=2; value<=20;
value++)
{
if(value%2==0)
{
array[i++] = value;
}
}
//display message on the computer
screen
System.out.println("The array is:
");
//display the array element on the
computer screen
for(int k=0; k<10; k++)
{
//display the element and one space
after that
System.out.print(array[k]+"
");
}
//method calling
reverse(array, 10);
//display the message on the
computer screen
System.out.println("\n\nThe reverse
array is: ");
//display the array element on the
computer screen
for(int k=0; k<10; k++)
{
//display the element and one space
after that
System.out.print(array[k]+"
");
}
}
}
OUTPUT:
The array is:
2 4 6 8 10 12 14 16 18 20
The reverse array is:
20 18 16 14 12 10 8 6 4 2