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
-109 <= ar[i] <= 109
Output Format
Print sum of all odd elements of the given array.
Inputcopy | Outputcopy |
---|---|
5 6 9 8 4 3 | 12 |
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();
}
}