Java Task 2 Please Dont Divide by Zero As your input ask th

Java

Task #2 – Please Don’t Divide by Zero

As your input, ask the user to enter two numbers: A and B

For your output:

display the results of A divided by B as long as B is NOT a zero

if B is zero, then display the error message “You cannot divide by zero”

Provide the source code.

Solution

DivideTest.java

import java.util.Scanner;


public class DivideTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter number A: \");
       int a = scan.nextInt();
       System.out.print(\"Enter number B: \");
       int b = scan.nextInt();
       if(b != 0){
           double result = a/(double)b;
           System.out.println(\"Result is \"+result);
       }
       else{
           System.out.println(\"You cannot divide by zero\");
       }
      
   }

}

Output:

Enter number A: 5
Enter number B: 2
Result is 2.5

Enter number A: 5
Enter number B: 0
You cannot divide by zero

Java Task #2 – Please Don’t Divide by Zero As your input, ask the user to enter two numbers: A and B For your output: display the results of A divided by B as l

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site