Question #224064

Write a program ―DivideByZeroǁ that takes two numbers a and b as input,

computes a/b, and invokes Arithmetic Exception to generate a message when the

denominator is zero.


Expert's answer

import java.util.Scanner;


public class DivideByZero {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a: ");
        double a=input.nextDouble();
        System.out.print("Enter a: ");
        double b=input.nextDouble();
        try{
          double result =a/b;  
          System.out.println("a/b = "+a+"/"+b+" = "+result);
        }catch(ArithmeticException ex){
          System.out.println("Division by zero");
        }
        
        input.close();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS