Question

In: Computer Science

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]

Solutions

Expert Solution

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


Related Solutions

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]
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...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to...
Java program problem 1 Come up with an idea for parallel arrays Create the arrays to hold 5 items in each In the main(), load the arrays with data Then output the entire contents of the arrays Create a method called find1() and pass the 2 arrays to that method. problem 2 In the find1() method, ask the user to enter a value to search for Write logic to search the first array and then output the related data from...
3. (Even or Odd) Write method IsEven that uses the remainder operator (%) to determine whether...
3. (Even or Odd) Write method IsEven that uses the remainder operator (%) to determine whether an integer is even. The method should take an integer argument and return true if the integer is even and false otherwise. Incorporate this method into an application that inputs a sequence of integers (one at time) and determines whether each is even or odd. (C# please)
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program that uses two identical arrays of at least 25 integers. It should call...
Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It...
In C++, write a program that uses two identical arrays of ten randomly ordered integers. It should display the contents of the first array, then call a function to sort it using the most efficient descending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using descending order selection sort, modified to print out the...
1/Write a C program that asks the user to enter 5 nums and adds each number...
1/Write a C program that asks the user to enter 5 nums and adds each number to a total. The program should then display the total. To add to the total, the program must call the function void sum(int value, int *ptotal) passing the value the user entered and the address of the total (a local variable in main). The function should add the value to the total. Note that the function is void and does not return anything. Don't...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT