In: Computer Science
Write a complete program called EvenNums that uses a
method to create arrays of even numbers starting 1 of the given length:
public static int[] getEvenNums(int length) {}
EX:
int[] nums = getEvenNumbers(6);
System.out.println(Arrays.toString(nums));
expected output:
[2, 4, 6, 8, 10, 12]
The code in java is given below.
import java.util.Arrays; import java.util.Scanner; public class EvenNums { public static int[] getEvenNumbers(int length) { int[] nums=new int[length]; int j=2; for(int i=0;i<length;i++) { nums[i]=j; j=j+2; } return nums; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int length; System.out.print("Enter the length of the array: "); length=sc.nextInt(); int[] nums=getEvenNumbers(length); System.out.println("The output array is"); System.out.println(Arrays.toString(nums)); } } The screenshot of the running code and output is given below.If the answer helped please upvote it means a lot. For any query please comment.