Exception:

ARITHMETIC EXCEPTION

Sample Input  and Output 1:

Enter the cost of the item for n days
100
Enter the value of n
 0
java.lang.ArithmeticException: / by zero

Sample Input and Output 2:

Enter the cost of the item for n days
100
Enter the value of n
20
Cost per day of the item is 5

CODE:


import java.util.Scanner;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cost,n, costPerDay;
System.out.println("Enter the cost of the item for n days");
cost=sc.nextInt();
System.out.println("Enter the value of n");
n=sc.nextInt();
try {
costPerDay=cost/n;
System.out.println("Cost per day of the item is "+costPerDay);
}catch(ArithmeticException arthExp) {
System.out.println(arthExp); //err- will show in red
}
}

}

Comments

Popular posts from this blog