Advertisement
Shekhar777

Java Loops and Introduction to Arrays - Post Class - Odd sum

Oct 19th, 2020
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. Q19-Given an array of N integers, your task is to calculate the sum of all the odd integers present in the array.
  2. Ans-import java.io.*;
  3. import java.util.*;
  4. class Main {
  5.     public static void main (String[] args) {
  6.        Scanner s=new Scanner(System.in);
  7.        int N=s.nextInt();
  8.        int arr[]=new int[N];
  9.        for(int i=0;i<N;i++){
  10.            arr[i]=s.nextInt();
  11.        }
  12.        int sum=0;
  13.        for(int j=0;j<N;j++){
  14.            
  15.          if(arr[j]%2!=0){
  16.                sum=sum+arr[j];             
  17.            }
  18.          
  19.        }
  20.        System.out.print(sum);
  21.        
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement