Question

In: Computer Science

Write a complete program called EvenNums that uses a method to create arrays of even numbers...

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]

Solutions

Expert Solution

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.

Related Solutions

Write program numsODD that uses the method to create arrays of odd nums starting 1 of...
Write program numsODD that uses the method to create arrays of odd nums starting 1 of the given length: public static int[] getnumsOdd(int length) {} int[] numbers = getnumsOdd(5); System.out.println(Arrays.toString(numbers)); output: [1, 3, 5, 7, 9]
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays....
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays. splice() must create the third array and return a pointer to main(). Main() must capture the pointer returned from the splice() function. Sol'n so far, I get the results of the first and second array, but it isn't showing the results of the spliced function: #include <iostream> using namespace std; // Function int* splice() inserts the 2nd array into 1st array starting at int...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Create a method evenOdd() that sorts an array into even numbers on one side and the...
Create a method evenOdd() that sorts an array into even numbers on one side and the odd numbers on the other side public class ourArray { int[] Ar; int size; public int getSize() {    return size; } public ourArray() {    Ar = new int[100];    size = 0; } public void add(int e) {    Ar[size]= e;    size++; } public int remove() {    //save the last item           int save = Ar[size -1];   ...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT