In: Computer Science
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]
JAVA PROGRAM
import java.util.*;
//numsODD class created
public class numsODD
{       //getnumsOdd method returns odd numbers array
        public static int[] getnumsOdd(int length) 
        {
                int[] numbers = new int[length]; 
                //Declare odd variable and initialize as 1
                int odd = 1;
                //Generate odd numbers by incrementing variable odd by 2
                for(int i = 0; i < length; i++)
                {
                        numbers[i] = odd;
                        odd += 2;
                }
                //return odd numbers of array
                return numbers;
        }
        public static void main(String[] args)
        {
                //call function
                int[] numbers = getnumsOdd(5);
                System.out.println(Arrays.toString(numbers));
        }
}
JAVA PROGRAM SCREENSHOT
OUTPUT
SCREENSHOT
