Solution Review: Adding Negative and Odd Numbers
The solution to the ‘Adding Negative and Odd Numbers’ challenge is discussed in this lesson.
We'll cover the following...
We'll cover the following...
Coding solution
Let’s take a look at the solution first.
Press + to interact
Java
public class NegAndOdd {public static void addition(int []arr) {int sum = 0;for (int i = 0; i < arr.length; i++ ){if(arr[i] % 2 != 0 && arr[i] < 0){sum += arr[i];}}System.out.println("\nThe sum of negative and odd numbers: " + sum);}public static void main(String []args) {int []arr = {-10, 15, -1, -8, 7};System.out.print("Array: ");for (int i:arr)System.out.print(i + " ");addition(arr);}}
...