Likes

Java Lab_10_Ans_1

Write a program to accept two numbers and perform division, the program should provide the functionality such that if a usertries to divide the number by 0, the program should terminate with a customized message.

SOLUTION





import java . util . Scanner;

public class Divison {

 public static void main (String args []) {
   
  int num1, num2, result;
  Scanner input = new Scanner (System . in);
  System.out.println ("Enter the 1st number");
  num1 = input . nextInt ();

 System.out.println ("Enter the 2nd number");

 num2 = input . nextInt ();

 try {

   result = num1 / num2;
   System.out.println ("The result is " + result);

 } catch (ArithmeticException e) {

 System,out.println ("Divison by zero not Possible! ");
  }

 }

}

No comments: