Java program to divide two numbers

This program divides two numbers in Java, using command line arguments. The dividend and divisor are read as input from the command line. The program uses a try catch statement to read the inputs and perform the division. The statement a=Integer.parseInt(s[0]); stores the first argument to the variable a and the statement b=Integer.parseInt(s[1]); stores the second argument to the variable b. If the divisor is 0 the program will throw an ArithmeticException also if there is any problem in reading the arguments the program throws an ArrayIndexOutOfBoundsException. Otherwise the program prints the result of the division to the output screen. 

Program


 

 // command line arguments in java

// Java program to find the quotient 

// Use command line arguments find the quotient of two numbers

// Demonstrate divide by zero exception

class Quotient
{ public static void main(String s[]) throws Exception { int a,b,c; try { a=Integer.parseInt(s[0]); // getting data b=Integer.parseInt(s[1]); // from command line try { c=a/b; System.out.println("Quotient is "+c); } catch(ArithmeticException ex) { System.out.println("Error in division !"); } } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array out of bound!"); } finally { System.out.println("End of the progarm!!!"); } } }


Output


java program to divide two numbers, command line

No comments: