Skip to content

Commit 5c1a7fa

Browse files
committed
Average between two numbers
1 parent 3cea6d2 commit 5c1a7fa

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

avg.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
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+
*/
217
import java.util.*;
318
public 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)

0 commit comments

Comments
 (0)