Sum Difference Multiply Divide in Java
Q. Write a Java program for addition, subtraction, multiplication, and division
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input two numbers from user
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
// Perform operations
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
// Handle division carefully to avoid division by zero
if (num2 != 0) {
double quotient = num1 / num2;
System.out.println("Quotient = " + quotient);
} else {
System.out.println("Division by zero is not allowed.");
}
// Display results
System.out.println("Sum = " + sum);
System.out.println("Difference = " + difference);
System.out.println("Product = " + product);
scanner.close();
}
}
Q. Write an algorithm for addition, subtraction, multiplication, and division
1. Start 2. Read two numbers and store them in num1 and num2 3. Compute sum = num1 + num2 4. Compute difference = num1 - num2 5. Compute product = num1 * num2 6. If num2 ≠ 0, compute quotient = num1 / num2 7. Display sum, difference, product, and quotient (if applicable) 8. If num2 = 0, display "Division by zero is not allowed" 9. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com