I – Sum of all odd elements

Print sum of all odd elements in an array.

Input Format

First line of input contains N – the size of the array and second line contains array elements.

Constraints

1 <= N <= 100
-10<= ar[i] <= 109

Output Format

Print sum of all odd elements of the given array.

InputcopyOutputcopy
5 6 9 8 4 312

Explanation 0

Self Explanatory

Solutions:

import java.util.Scanner;

public class SumOfOddElements {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int sum = 0;
        for (int i = 0; i < N; i++) {
            int num = scanner.nextInt();
            if (num % 2 != 0) {
                sum += num;
            }
        }
        System.out.println(sum);
        scanner.close();
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top