Başar Ballıöz
Başar Ballıöz

Reputation: 109

How to fix my Even-Odd Program java code it writes even or odd but also the number

package practıce1.array;
public class PRACTICE1ARRAY {
    public static int evenorodd(int a) {
    @author Başar Ballıöz
        if (a % 2 == 0) {
            System.out.println("Number Is Even");
        }
        else {
            System.out.println("Number Is Odd");
        }
        return a;
    }

    public static void main(String[] args) {

       int[] arr = new int[] {2,7,9,10,22,31};
       for (int i = 0; i < arr.length; i++) {
            System.out.println("Element Of Index " + i + " = " + arr[i]);
        }    
        System.out.println(evenorodd(arr[0]));    
    }
}

Output:

run:
Element Of Index 0 = 2
Element Of Index 1 = 7
Element Of Index 2 = 9
Element Of Index 3 = 10
Element Of Index 4 = 22
Element Of Index 5 = 31
Number Is Even
2
BUILD SUCCESSFUL (total time: 0 seconds)

I dont want to see value of number. I just want to see is number even or odd ? I think it`s causing because of a mistake which is happened in method

Upvotes: 2

Views: 94

Answers (1)

mypetlion
mypetlion

Reputation: 2524

Your evenorodd doesn't need to return the number given to it then, and you don't need to print the value given in. Change your method to this:

public static void evenorodd(int a) {
    if (a % 2 == 0) {
        System.out.println("Number Is Even");
    }
    else {
       System.out.println("Number Is Odd");
    }
}

And when you call it, don't put the method call inside a print statement.

Change this:

System.out.println(evenorodd(arr[0]));

to this:

evenorodd(arr[0])

Upvotes: 2

Related Questions