Question #5037

You are given a table that shows the total number of cars sold by a local car manufacturer in Johor Bharu for the first six months in the year 2008.

Month Jan Feb Mar Apr May Jun
Number of cars sold (units) 191 196 269 308 236 145
Based on the figure given above, you are required to write a Java program using array to store all the information given above and a for loop to determine the sum of all the odd numbers in the array. Finally, the program should display the result.

Expert's answer

Task

JAVA

You are given a table that shows the total number of cars sold by a local car manufacturer in Johor Bharu for the first six months in the year 2008.

Month Jan Feb Mar Apr May Jun

Number of cars sold (units) 191 196 269 308 236 145

Based on the figure given above, you are required to write a Java program using array to store all the information given above and a for loop to determine the sum of all the odd numbers in the array. Finally, the program should display the result.

Solution

public class Main {
    public static void main(String[] args) {
        int[] units = {191, 196, 269, 308, 236, 145};
        int result = 0;
        for (int i = 0; i < units.length; i++) {
            if (units[i] % 2 != 0) {
                result += units[i];
            }
        }
        System.out.println("Result: " + result);
    }
}

Answer

Result: 605

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS