Question
Write a java program to perform the four arithmetic operation using function.
Answers
There are no human answers yet.
Answered by
GPT 3.5
Here's an example of a Java program that performs the four arithmetic operations using functions:
```java
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.println("Addition: " + add(num1, num2));
System.out.println("Subtraction: " + subtract(num1, num2));
System.out.println("Multiplication: " + multiply(num1, num2));
System.out.println("Division: " + divide(num1, num2));
scanner.close();
}
public static double add(double num1, double num2) {
return num1 + num2;
}
public static double subtract(double num1, double num2) {
return num1 - num2;
}
public static double multiply(double num1, double num2) {
return num1 * num2;
}
public static double divide(double num1, double num2) {
return num1 / num2;
}
}
```
In this program, we have four functions `add`, `subtract`, `multiply`, and `divide` that perform the respective arithmetic operations using the given input numbers. The `main` function takes two numbers as input from the user, calls these functions with the input numbers, and displays the results of the operations.
```java
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.println("Addition: " + add(num1, num2));
System.out.println("Subtraction: " + subtract(num1, num2));
System.out.println("Multiplication: " + multiply(num1, num2));
System.out.println("Division: " + divide(num1, num2));
scanner.close();
}
public static double add(double num1, double num2) {
return num1 + num2;
}
public static double subtract(double num1, double num2) {
return num1 - num2;
}
public static double multiply(double num1, double num2) {
return num1 * num2;
}
public static double divide(double num1, double num2) {
return num1 / num2;
}
}
```
In this program, we have four functions `add`, `subtract`, `multiply`, and `divide` that perform the respective arithmetic operations using the given input numbers. The `main` function takes two numbers as input from the user, calls these functions with the input numbers, and displays the results of the operations.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.