-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop31.java
52 lines (45 loc) · 1.72 KB
/
oop31.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.InputMismatchException;
import java.util.Scanner;
public class oop31 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[100];
int n, m, p;
try {
System.out.println("Enter the number of elements in array");
n = sc.nextInt();
for (int i = 0; i < n; i++) {
System.out.print("Enter the number at index " + i + " = ");
arr[i] = sc.nextInt();
}
try {
System.out.println("Enter the index for array element you want to divide");
m = sc.nextInt();
if (m < 0 || m >= n) {
System.out.println("Invalid index. Please enter a valid index.");
sc.close();
return;
}
System.out.println("Enter the number you want to divide it by");
p = sc.nextInt();
if (p == 0) {
System.out.println("Division by zero is not allowed.");
sc.close();
return;
}
System.out.println("The value at index " + m + " = " + arr[m]);
System.out.println("The result of division = " + arr[m] / p);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Out of bond");
}
catch (ArithmeticException e) {
System.out.println("Arithmetic error: " + e.getMessage());
} finally {
sc.close();
}
} catch (InputMismatchException i) {
System.out.println("Enter valid Input");
}
}
}