Build a small program that reads numbers from the user and prints the results of all arithmetic operations.
import java.util.Scanner;
public class ArithmeticEvaluator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
System.out.println("Sum: " + (a + b));
System.out.println("Difference: " + (a - b));
System.out.println("Product: " + (a * b));
System.out.println("Quotient: " + (a / b));
System.out.println("Remainder: " + (a % b));
scanner.close();
}
}
Try running this with inputs like 10 and 3. Then try 10 and 0. Dividing by will crash your program with an ArithmeticException. You'll learn how to handle that in a later section.