-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
29 lines (23 loc) · 911 Bytes
/
Copy pathExample2.java
File metadata and controls
29 lines (23 loc) · 911 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
import java.util.Scanner;
public class Example2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] numbers = {10, 20, 30}; // Small array
System.out.println("Enter two numbers:");
int numerator = sc.nextInt();
int denominator = sc.nextInt();
System.out.println("Enter an index (0 to 2):");
int index = sc.nextInt();
try {
System.out.println("Division result: " + (numerator / denominator));
System.out.println("Array value: " + numbers[index]);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero!");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Invalid index!");
} finally {
System.out.println("Finally block executed.");
sc.close();
}
}
}