user3019459
user3019459

Reputation: 1

Java - Finding even numbers and the percent of them within an array

This is the question that I need to figure out:

Write a method called percentEven that accepts an array of integers as a parameter and returns the percentage of even numbers in the array as a real number. For example, if the array stores the elements [6, 2, 9, 11, 3] then your method should return 40.0. If the array contains no even elements or no elements at all, return 0.0.

Here is what I have so far:

import java.util.*;

public class Change {
    public static void main(String[] args) {


    Scanner console = new Scanner(System.in);

    System.out.println("Let's find the range.");
    System.out.println("Enter five numbers to find the range.");

    int num = console.nextInt();
    int[] list = new int[num];

    System.out.println("The numbers you entered are: " + list.length); 
    System.out.println();

    percentEven(list);

    }

    public static void percentEven(int[] num){

        int percent = 0;
        int even = 0;

        for(int i = 0; i < num.length; i++){
            if(num[i] % 2 == 0){
            even++;
            }

        }  

      percent = (even / num.length) *100;

        System.out.println("The percent of even numbers is: " + percent);      
    }


}

When I run it, I get 100 as the percent.

Upvotes: 0

Views: 3656

Answers (5)

hodan_egal
hodan_egal

Reputation: 67

import java.util.*;
public class percentEvenClass
{
  public static void main(String[] args){
    int[] list = {6, 2, 9, 11, 3};
    int percentEven_Result = percentEven(list);
    System.out.println(percentEven_Result);
  }
  public static int percentEven(int[] list){

    int count = 0;
    int percent = 0;
    for (int i=0; i<list.length; i++){

        if (list[i] % 2 == 0){
            count++;
        }
        percent = (count * 100)/ list.length;

 }
  return percent;  

}

}

Upvotes: 0

isnot2bad
isnot2bad

Reputation: 24454

There are three major problems in your code:

  1. You are reading only ONE integer, not five
  2. You use this single integer to define the LENGTH of the array, not the content (so you don't put the integer into the array. So the array contains only zeroes, which means that all of them are even.
  3. You are doing wrong integer arithmetic (as Obicere already stated in his answer). But this doesn't have any effect, as all elements of the array are even, so the result will be 100 in any case.

Upvotes: 2

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79848

Write

percent = even * 100 / num.length;

Changing the order of the operations will make the integer division business work in your favour - you'll get a value rounded down to the next lowest percentage, rather than rounded down to zero.

Also fix the problem with all the numbers being zero by reading them from the keyboard, as in tintinmj's answer.

Upvotes: 0

You are almost there. But you are initializing and storing the array wrong way. Do this

int num = console.nextInt();
int[] list = new int[num];
System.out.println("Enter " + num + " numbers");
for(int i = 0; i < num; i++) {
    list[i] = console.nextInt();
}
System.out.println("The numbers you entered are: " + java.util.Arrays.toString(list)); 
System.out.println();

and also do as other suggested.

Upvotes: 1

Obicere
Obicere

Reputation: 3019

Two issues here:

Cast one of them to a double or float.

percent = (even / (double) num.length) *100;

The other issue is that you never assign the numbers any value, so they are all 0. 0 % 2 is equal to 0, so the list is, by definition, 100% even.


You should also have a base case in the method when nums == {}, which would return 0.0 as the assignment states.

Upvotes: 5

Related Questions