Develop a Java program that prompts the user for two numbers (x and y) and displays the result of the division operation.
The program should include a check to prevent division by zero using an if statement. If the user attempts to divide by zero, a message will be displayed indicating "Division by zero is not possible".
if
6 0
Division by zero is not possible
import java.util.Scanner; public class DivisionByZero { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int x = scanner.nextInt(); System.out.print("Enter the second number: "); int y = scanner.nextInt(); if (y == 0) { System.out.println("Division by zero is not possible"); } else { int division = x / y; System.out.println(x + " / " + y + " = " + division); } scanner.close(); } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Write a program in Java that requests an integer x from the user and print the result of adding it to the square.
Write a program in Java that multiplies by three factors.
Write a program in Java that requests two integers from the user (x, y) and print the result of the division and the rest of the division on a new line.
Write a program in Java that calculates the result of adding, subtracting, multiplying and dividing two numbers entered by the user.
Create a program in Java for calculate the surface and volume of a sphere, given its radius.
Understand the rules of operator precedence in Java and how they influence the results of your calculations.
Learn how to calculate the arithmetic mean of a set of numbers and how to use it in data analysis.
Explore how to perform equivalent mathematical operations to simplify expressions.
The Celsius scale is centigrade, 100 divisions separate the freezing point from the boiling point of water.
Apply your mathematical and programming knowledge to calculate function values in Java.
Practice with easy and intermediate Java exercises on arithmetic operations and some types of numerical data.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.