lundi 7 septembre 2020

programming Java, Find Highest odd number in an array and return 0 if no odd number present

How to find highest odd number in the array and prints that to the standard output. It should return 0 if the array has no odd numbers.

I have written the program but not getting how to return 0 in case no odd number in array.

below is the code:

public class Highestodd_number {

public int odd() {
    int[] arr = { 4, 6, 9};

    int max = arr[0];

    for (int i = 0; i < arr.length; i++) {

        if (arr[i] % 2 != 0 && arr[i] > max) {

            max = arr[i];
            
            System.out.println(max);

        }

    }

    
    return 0;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Highestodd_number rt = new Highestodd_number();
    rt.odd();
}

}

Aucun commentaire:

Enregistrer un commentaire