-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvgOfValueForOddKey.java
More file actions
32 lines (32 loc) · 922 Bytes
/
AvgOfValueForOddKey.java
File metadata and controls
32 lines (32 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
class AvgOfValueForOddKey{
public static int avgValue(Map<Integer, Integer> map){
Iterator<Map.Entry<Integer, Integer>> itr=map.entrySet().iterator();
int val=0, avg=0, sum=0, turn=0;
while(itr.hasNext()){
Map.Entry<Integer, Integer> entry=itr.next();
if(entry.getKey()%2!=0){
val=entry.getValue();
sum=sum+val;
turn+=1;
}
}
avg=sum/turn;
return avg;
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
Map<Integer, Integer> obj=new HashMap<Integer, Integer>();
System.out.println("Enter no. of entries..");
int n=s.nextInt();
System.out.println("Enter "+n+" RegNum and Marks..");
for(int i=0;i<n;i++){
int k=s.nextInt();
int v=s.nextInt();
obj.put(k, v);
}
System.out.println(obj);
int avg=avgValue(obj);
System.out.println("\nThe average of marks whose key is odd -> "+avg);
}
}