Question:
Write a Java program that takes two integer inputs num1 and num2 from the user. The program should perform the following operations and display the results:
num1 and num2.num1 and num2.num1 and num2.num1 divided by num2 (integer division).num1 divided by num2.Write the program for the above requirements and test it by taking num1 = 10 and num2 = 3.
Answer:
import java.util.Scanner;
public class OperatorsAndExpressions {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value for num1: ");
int num1 = scanner.nextInt();
System.out.print("Enter the value for num2: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;
int remainder = num1 % num2;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
}
}
Explanation:
Scanner class from the java.util package to read user input.Scanner object to read the input from the user.num1 using System.out.print statement.num1 using scanner.nextInt() method.num2.num2.num1 and num2 using the + operator and stores the result in the variable sum.num1 and num2 using the - operator and stores the result in the variable difference.num1 and num2 using the * operator and stores the result in the variable product.num1 divided by num2 using the / operator and stores the result in the variable quotient.num1 divided by num2 using the % operator and stores the result in the variable remainder.System.out.println statements.