Question

Please write a Java program:Given an array of positive integers, return a count of the number of even integers. countEvens([2, 3, 5])-1 countEvens([4, 20

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int countEvens(int[] nums){
  if(nums == null){
    return 0;
  }
  int count = 0;
  for(int i = 0;i<nums.length;i++){
    if(nums[i]%2==0){
      count += 1;
    }
  }
  return count;
}

Please up vote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
Please write a Java program: Given an array of positive integers, return a count of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with these java codingbat questions please: Create a method that creates a user...

    I need help with these java codingbat questions please: Create a method that creates a user id, where the user id is made up of the first letter of the first name and the entire last name. e.g. john smith ->jsmith getUserld("bill", "gates")-"bgates" getUserId("steve", "jobs")"sjobs" getUserId("larry", "page") -"lpage" Go Save, Compile, Run (ctrl-enter) public String getUserId( String firstName, String lastName) Given a number n, create an array with elements starting from 1 to n. e.g. 5 > getNumArray(1) [1] getNumArray...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • \ Please use JAVA to solve this problem. Thanks! Given a non-negative integer, return true if...

    \ Please use JAVA to solve this problem. Thanks! Given a non-negative integer, return true if most of its digits are odd (more digits are odd than are even). An odd number is any number ending with 1, 3, 5, 7, or 9 mostlyOdd(47) false mostlyOdd(79) → true mostlyOdd(286) - false Go Save, Compile, Run (ctrl-enter) boolean mostlyOdd(int num)

  • X43: countEvens Write a function in Java that takes an int array as its parameter and...

    X43: countEvens Write a function in Java that takes an int array as its parameter and returns the number of even ints it contains. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. Complete the code bellow. Complete the code below. Use: logic conditionals if arrays loops mod public int countEvens(int[] nums) { }

  • Write a function with two input parameters: an array of numbers and the size of the...

    Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Python: problem 1 Given an array of integers, return the sum of two indices from this...

    Python: problem 1 Given an array of integers, return the sum of two indices from this array based on input parameters, x and y. Example: Given nums = [2, 7, 11, 15], x = 3, y = 1 Because nums[x] + nums[y] = 15 + 7 = 22, return 22. You may use this as a template for your code class Solution: def two_sum(self, nums: List[int], x: int, y: int) -> int:

  • Problem 4: Write a Java program that reads positive integers, find the largest of them, count...

    Problem 4: Write a Java program that reads positive integers, find the largest of them, count its occurrences and print out the average of all input integers up to two decimal places (小數 點第二位). Use the characters·Q' or'q, to end the input. Below are two sample runs: Enter an integer, or quit with Q or q:1 Enter an integer, or quit with Q or q: 2 Enter an integer, or quit with Q or q: 3 Enter an integer, or...

  • Has to be in Java programming language Java Python mirrorLength We'll say that a "mirror" section...

    Has to be in Java programming language Java Python mirrorLength We'll say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array. 3 mirrorLength([1, 2, 3, 8, 9,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT