File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- package Java ;
1+ /* Find average of a number array between two limit numbers (except the two numbers)
2+ * Input
3+ * ---------------
4+ * 5
5+ 1
6+ 2
7+ 3
8+ 4
9+ 5
10+ * 1
11+ * 4
12+ *
13+ * Output
14+ * ---------------
15+ * 2
16+ */
217import java .util .*;
318public class avg
419{
520 public static void main (String [] args ) {
621 Scanner sc = new Scanner (System .in );
7- int [] arr = new int [7 ];
8- for (int i =0 ; i <7 ; i ++)
22+ int n = sc .nextInt ();
23+ int [] arr = new int [n ];
24+ for (int i =0 ; i <n ; i ++)
925 {
1026 arr [i ] = sc .nextInt ();
1127 }
1228 int low = sc .nextInt ();
1329 int up = sc .nextInt ();
1430
1531 int lowerIndex = index (arr , low );
16- if (lowerIndex ==0 )
17- {
18- System .out .println ("Index not found" );
19- }
32+
2033 int upperIndex = index (arr , up );
2134 if (upperIndex ==0 )
2235 {
2336 System .out .println ("Index not found" );
2437 }
2538 int sum =0 , count =0 ;
26- for ( int i = lowerIndex + 1 ; i < upperIndex ; i ++ )
39+ if ( upperIndex > 0 )
2740 {
28- sum = sum +arr [i ];
29- count ++;
41+ for (int i =lowerIndex +1 ; i <upperIndex ; i ++)
42+ {
43+ sum = sum +arr [i ];
44+ count ++;
45+ }
46+ System .out .println (sum /count );
3047 }
31- System . out . println ( sum / count );
48+
3249 }
3350
3451 public static int index (int [] arr , int num )
You can’t perform that action at this time.
0 commit comments