Question
Write a program to perform four arithmetic operations using function if statement ,loops.....OOP in c++/java
Answers
There are no human answers yet.
Answered by
GPT 3.5
Sure! Here's an example program in C++ that performs four arithmetic operations using conditional statements, loops, and Object-Oriented Programming (OOP) concepts:
```cpp
#include <iostream>
using namespace std;
class ArithmeticOperations {
public:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
double divide(int a, int b) {
if (b != 0) {
return static_cast<double>(a) / b;
} else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
};
int main() {
ArithmeticOperations calculator;
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Arithmetic Operations:" << endl;
cout << "----------------------" << endl;
cout << "Addition: " << calculator.add(num1, num2) << endl;
cout << "Subtraction: " << calculator.subtract(num1, num2) << endl;
cout << "Multiplication: " << calculator.multiply(num1, num2) << endl;
cout << "Division: " << calculator.divide(num1, num2) << endl;
return 0;
}
```
This program defines a class `ArithmeticOperations` with member functions for each arithmetic operation: `add`, `subtract`, `multiply`, and `divide`. The `divide` function checks for division by zero before performing the operation to avoid runtime errors.
In the `main` function, an object of the `ArithmeticOperations` class is created. The user is prompted to enter two numbers, and the program performs the four arithmetic operations using the object's member functions. The results are displayed on the console.
Note: This program assumes valid input from the user and does not handle input validation.
```cpp
#include <iostream>
using namespace std;
class ArithmeticOperations {
public:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
double divide(int a, int b) {
if (b != 0) {
return static_cast<double>(a) / b;
} else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
};
int main() {
ArithmeticOperations calculator;
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Arithmetic Operations:" << endl;
cout << "----------------------" << endl;
cout << "Addition: " << calculator.add(num1, num2) << endl;
cout << "Subtraction: " << calculator.subtract(num1, num2) << endl;
cout << "Multiplication: " << calculator.multiply(num1, num2) << endl;
cout << "Division: " << calculator.divide(num1, num2) << endl;
return 0;
}
```
This program defines a class `ArithmeticOperations` with member functions for each arithmetic operation: `add`, `subtract`, `multiply`, and `divide`. The `divide` function checks for division by zero before performing the operation to avoid runtime errors.
In the `main` function, an object of the `ArithmeticOperations` class is created. The user is prompted to enter two numbers, and the program performs the four arithmetic operations using the object's member functions. The results are displayed on the console.
Note: This program assumes valid input from the user and does not handle input validation.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.