Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function named oddsEvens that takes in a vector of positive integers and

ID: 3780350 • Letter: W

Question

Write a function named oddsEvens that takes in a vector of positive integers and returns (via the parameters) how many of those integers are odd and how many are even.

For example, if the vector had the following values {6, 5, 2, 10, 11, 4}, then the function would set the evens count to 4, and the odds count to 2. Obviously, if the vector is passed in empty, both counts will be set to 0.

Give an example of the invocation of this function; you must show the declarations of any variables needed for this purpose, but you do not need to "populate" them - i.e. do not show where they are assigned values.

Explanation / Answer

package com.exm;

import java.util.*;

/*

* v is an object of vector

* e is an object of Enumeration

* even is a variable for even numbers

* odd is a variable for odd numbers

* z is a variable which stores iteration value

*/

public class oddsEvens{

public static void main(String args[])

{

Vector<Integer> v=new Vector<Integer>();

v.add(1);

v.add(12);

v.add(10);

v.add(15);

v.add(16);

Enumeration<Integer> e=v.elements();

int even =0;

int odd=0;

while(e.hasMoreElements()){

int z= e.nextElement();

if(z%2==0){

even++;

}else{

odd++;

}

}

System.out.println(" no of even numbers:"+even);

System.out.println("no of odd numbers:"+odd);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote