C++ Program to Perform Arithmetic Operations on Two Given Numbers
C++ program to perform arithmetic operations on two given numbers.
In this article, you will learn how to write a c++ program to perform arithmetic operations on two given numbers.
Example
6
8
SUM 6 + 8 = 14
DIFFERENCE 6 - 8 = -2
PRODUCT 6 * 8 = 48
QUOTIENT 6 / 8 = 0.75
MODULUS 6 % 8 = 6
You should have the knowledge of the following topics in c++ programming to understand this program.
- C++ Arithmetic operators
- C++
main()
function - C++
cin
object - C++
cout
object - C++ Oops
- C++
Switch
statement - C++ Functions
There we will perform these arithmetic operations like Sum, Difference, Multiplication, Division, and Modulus.
In this article, we will solve this problem in five methods:
- Using the normal calculations
- Using the objects and classes
- Using the switch case
- Using the functions
- Using the inline function
Source Code
// C++ Program to Perform Arithmetic Operations on Two Given Numbers
#include <bits/stdc++.h>
using namespace std;
int main() {
int p, q;
int sum, sub, mul, mod;
float div;
cout << "Enter any two numbers:\n";
cin >> p >> q;
// It will perform all arithmetic operations
sum = p + q;
sub = p - q;
mul = p * q;
div = (float)p / q;
mod = p % q;
// It will print the final output of all arithmetic operations
cout << endl;
cout << "SUM " << p << " + " << q << " = " << sum << "\n";
cout << "DIFFERENCE " << p << " - " << q << " = " << sub << "\n";
cout << "PRODUCT " << p << " * " << q << " = " << mul << "\n";
cout << "QUOTIENT " << p << " / " << q << " = " << div << "\n";
cout << "MODULUS " << p << " % " << q << " = " << mod << "\n";
return 0;
}
Output
Enter any two positive integer numbers:
6
8
SUM 6 + 8 = 14
DIFFERENCE 6 - 8 = -2
PRODUCT 6 * 8 = 48
QUOTIENT 6 / 8 = 0.75
MODULUS 6 % 8 = 6
Explanation
In this given program, we have taken inputs 6
and 8
from the user then we applied arithmetic operations on these integer numbers.
Then this will be returned the 14
, -2
, 48
, 0.75
, and 6
output values of this program.
C++ Program to Perform Arithmetic Operations using Objects and Classes
// C++ Program to Perform Arithmetic Operations using Objects and Classes
#include <bits/stdc++.h>
using namespace std;
class ArithmeticOperations {
public:
void Sum(int a, int b) {
int sum = a + b;
cout << "SUM " << a << " + " << b << " = " << sum << "\n";
}
void Difference(int a, int b) {
int sub = a - b;
cout << "DIFFERENCE " << a << " - " << b << " = " << sub << "\n";
}
void Multiplication(int a, int b) {
int mul = a * b;
cout << "PRODUCT " << a << " * " << b << " = " << mul << "\n";
}
void Quotient(int a, int b) {
float div = (float)a / b;
cout << "QUOTIENT " << a << " / " << b << " = " << div << "\n";
}
void Modules(int a, int b) {
int mod = a % b;
cout << "MODULUS " << a << " % " << b << " = " << mod << "\n";
}
};
// It's the driver function
int main() {
int p, q;
cout << "Enter any two numbers:\n";
cin >> p >> q;
// To make the instances of `ArithmeticOperations` class
ArithmeticOperations AO;
// It will print the final output of all arithmetic operations
cout << endl;
AO.Sum(p, q);
AO.Difference(p, q);
AO.Multiplication(p, q);
AO.Quotient(p, q);
AO.Modules(p, q);
return 0;
}
Output
Enter any two numbers:
12
15
SUM 12 + 15 = 27
DIFFERENCE 12 - 15 = -3
PRODUCT 12 * 15 = 180
QUOTIENT 12 / 15 = 0.8
MODULUS 12 % 15 = 12
C++ Program to Perform Arithmetic Operations using Switch Case
// C++ Program to Perform Arithmetic Operations using Switch Case
#include <bits/stdc++.h>
using namespace std;
int main() {
int p, q, opt;
cout << "Enter any two numbers:\n";
cin >> p >> q;
cout << "\n######## Choose a option from these ########\n\n";
cout << "1 - To do sum operation on two numbers\n";
cout << "2 - To do differences operation on two numbers\n";
cout << "3 - To do multiplication operation on two numbers\n";
cout << "4 - To do division operation on two numbers\n";
cout << "5 - To do modulus operation on two numbers\n\n";
cin >> opt;
cout << endl;
// It will perform the all arithmetic operations on the basis of selected option
switch (opt) {
case 1: {
int sum = p + q;
cout << "SUM:\t" << p << " + " << q << " = " << sum << "\n";
break;
}
case 2: {
int sub = p - q;
cout << "DIFFERENCE:\t" << p << " - " << q << " = " << sub << "\n";
break;
}
case 3: {
int mul = p * q;
cout << "PRODUCT:\t" << p << " * " << q << " = " << mul << "\n";
break;
}
case 4: {
float div = (float)p / q;
cout << "QUOTIENT:\t" << p << " / " << q << " = " << div << "\n";
break;
}
case 5: {
int mod = p % q;
cout << "MODULUS:\t" << p << " % " << q << " = " << mod << "\n";
break;
}
default:
cout << "You choosed the wrong option!\n";
}
return 0;
}
Output
Enter any two numbers:
12
15
######## Choose a option from these ########
1 - To do sum operation on two numbers
2 - To do differences operation on two numbers
3 - To do multiplication operation on two numbers
4 - To do division operation on two numbers
5 - To do modulus operation on two numbers
1
SUM: 12 + 15 = 27
C++ Program to Perform Arithmetic Operations using Functions
// C++ Program to Perform Arithmetic Operations using Functions
#include <bits/stdc++.h>
using namespace std;
// These functions will perform the all arithmetic operations
void Sum(int p, int q) {
int sum = p + q;
cout << "SUM " << p << " + " << q << " = " << sum << "\n";
}
void Difference(int p, int q) {
int sub = p - q;
cout << "DIFFERENCE " << p << " - " << q << " = " << sub << "\n";
}
void Multiplication(int p, int q) {
int mul = p * q;
cout << "PRODUCT " << p << " * " << q << " = " << mul << "\n";
}
void Division(int p, int q) {
float div = (float)p / q;
cout << "QUOTIENT " << p << " / " << q << " = " << div << "\n";
}
void Modulus(int p, int q) {
int mod = p % q;
cout << "MODULUS " << p << " % " << q << " = " << mod << "\n";
}
// It's the driver function
int main() {
int p, q;
cout << "Enter any two numbers:\n";
cin >> p >> q;
// It will perform the all arithmetic operations
cout << endl;
Sum(p, q);
Difference(p, q);
Multiplication(p, q);
Division(p, q);
Modulus(p, q);
return 0;
}
C++ Program to Perform Arithmetic Operations using Inline Function
// C++ Program to Perform Arithmetic Operations using Inline Function
#include <bits/stdc++.h>
using namespace std;
// These functions will perform the all arithmetic operations
inline void Sum(int p, int q) {
int sum = p + q;
cout << "SUM " << p << " + " << q << " = " << sum << "\n";
}
inline void Difference(int p, int q) {
int sub = p - q;
cout << "DIFFERENCE " << p << " - " << q << " = " << sub << "\n";
}
inline void Multiplication(int p, int q) {
int mul = p * q;
cout << "PRODUCT " << p << " * " << q << " = " << mul << "\n";
}
inline void Division(int p, int q) {
float div = (float)p / q;
cout << "QUOTIENT " << p << " / " << q << " = " << div << "\n";
}
inline void Modulus(int p, int q) {
int mod = p % q;
cout << "MODULUS " << p << " % " << q << " = " << mod << "\n";
}
// It's the driver function
int main() {
int p, q;
cout << "Enter any two numbers:\n";
cin >> p >> q;
// It will perform the all arithmetic operations
cout << endl;
Sum(p, q);
Difference(p, q);
Multiplication(p, q);
Division(p, q);
Modulus(p, q);
return 0;
}
Output
Enter any two numbers:
5
9
SUM 5 + 9 = 14
DIFFERENCE 5 - 9 = -4
PRODUCT 5 * 9 = 45
QUOTIENT 5 / 9 = 0.555556
MODULUS 5 % 9 = 5
Also, visit these links
C Program to Enter Two Numbers and Perform All Arithmetic Operations