-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ_2_MathOperation.java
29 lines (25 loc) · 1.02 KB
/
Q_2_MathOperation.java
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
/*Write a class MathOperation which takes 5 integers inputs from user. Create an array using these parameters. Loop through the array and obtain the sum and average of all the elements and display the result.
Various exceptions that may arise like ArithmeticException, NumberFormatException, and so on should be handled.*/
import java.util.InputMismatchException;
import java.util.Scanner;
public class Q_2_MathOperation {
public static void main(String[] args) {
int[] arr = new int[5];
int sum=0;
double avg=0;
Scanner sc = new Scanner(System.in);
try {
for (int i = 0; i < 5; i++) {
arr[i] = sc.nextInt();
}
for (int i = 0; i < 5; i++) {
sum+=arr[i];
}
System.out.println(sum);
System.out.println(avg/5);
}
catch (ArithmeticException|IndexOutOfBoundsException| InputMismatchException|NumberFormatException e){
System.out.println(e);
}
}
}