-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmetic Operations.java
More file actions
56 lines (46 loc) · 1.62 KB
/
Arithmetic Operations.java
File metadata and controls
56 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
public class ArithmeticOperations {
// Method to add two numbers
public static double add(double a, double b) {
return a + b;
}
// Method to subtract two numbers
public static double subtract(double a, double b) {
return a - b;
}
// Method to multiply two numbers
public static double multiply(double a, double b) {
return a * b;
}
// Method to divide two numbers
public static double divide(double a, double b) {
if (b != 0) {
return a / b;
} else {
System.out.println("Error: Division by zero is not allowed.");
return Double.NaN; // Return NaN for division by zero
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter two numbers
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
// Perform arithmetic operations
double sum = add(num1, num2);
double difference = subtract(num1, num2);
double product = multiply(num1, num2);
double quotient = divide(num1, num2);
// Display the results
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
if (!Double.isNaN(quotient)) {
System.out.println("Quotient: " + quotient);
}
// Close the scanner
scanner.close();
}
}