Find even odd elements in array in java

Write a java program to find the number of even and odd elements in array and perform the addition of even and odd elements separately.

package array;

import java.util.*;
public class sumofevenodd 
{
    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int size,i,even=0,odd=0,sum1=0,sum2=0;
        System.out.println("enter size");
        size=s.nextInt();
        int a[]=new int[size];
        for(i=0;i<size;i++)
        {
            a[i]=s.nextInt();
            if(a[i]%2==0)
            {
               even++;
               sum1=sum1+a[i];
            }
            else
            {
                odd++;
                sum2=sum2+a[i];
            }
        }
        System.out.println("Total even are::"+even);
        System.out.println("Total odd are::"+odd);
        System.out.println("sum of even are::"+sum1);
        System.out.println("sum of odd are::"+sum2);
    }
}

Leave a Reply

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