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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Output
6
11 22 33 44 55 66
77
6
11 22 33 44 55 66
77
Comments
Post a Comment