Find the even and odd number in an array,take the average of both and add them

Find the even and odd number in an array,take the average of both and add them
import java.util.*;
public class PracticeDemo {
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
String arraySize=sc.nextLine();
int size=Integer.parseInt(arraySize);
String elements=sc.nextLine();
String [] array=new String [size];
int j=0,x=0,even=0,odd=0,evenCount=0,oddCount=0;
for(int i=0;i<elements.length();i++){
if(String.valueOf(elements.charAt(i)).equals(" ")){
array[j]=elements.substring(x,i);
x=i+1;
j++;
}
}
array[j]=elements.substring(x,elements.length());
for(int k=0;k<array.length;k++){
int a=Integer.parseInt(array[k]);
if(a%2==0){
even+=a;
evenCount++;
}else{
odd+=a;
oddCount++;
}
}
System.out.print((int)(even/evenCount+odd/oddCount));
sc.close();
}
}
view raw PostDraft2 hosted with ❤ by GitHub
Output
6
11 22 33 44 55 66
77

Comments

Popular posts from this blog

Reasoning-Number Series

Profit and Loss

Reasoning-Letter Series