CODE:
import java.util.Scanner;
class Student {
void calculateAverage(int totalMarks, int numberOfSubjects) {
if (totalMarks == 0 || numberOfSubjects == 0) {
throw new ArithmeticException("Marks or subjects cannot be zero");
}
double avg = (double) totalMarks / numberOfSubjects;
System.out.println("Average marks: " + avg);
}
}
public class MarksValidationDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student s = new Student();
System.out.print("Enter total marks: ");
int marks = sc.nextInt();
System.out.print("Enter number of subjects: ");
int subjects = sc.nextInt();
try {
s.calculateAverage(marks, subjects);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}OUTPUT:
Enter total marks: 100
Enter number of subjects: 3
Average marks: 33.333333333333336
Enter total marks: 0
Enter number of subjects: 0
Error: Marks or subjects cannot be zero