Here is a C++ program to add, subtract, multiply and divide two numbers. In this program, we will learn about basic arithmetic operators in C++ programming language.
Arithmetic operators in C++ are used to perform mathematical operations. There are five fundamental arithmetic operators supported by C++ language, which are
addition(+), subtraction(-), division(/), multiplication(-), and modulus(%) of two numbers.
| Operator | Description | Syntax | Example |
|---|---|---|---|
| + | Adds two numbers | a + b | 15 + 5 = 20 |
| - | Subtracts two numbers | a - b | 15 - 5 = 10 |
| * | Multiplies two numbers | a * b | 15 * 5 = 75 |
| / | Divides numerator by denominator | a / b | 15 / 5 = 3 |
| % | Returns remainder after an integer division | a % b | 15 % 5 = 0 |
C++ Program to Perform Addition Subtraction Multiplication Division of Two Numbers
#include <iostream>
using namespace std;
int main(){
/* Variable declation */
int x, y;
int sum, difference, product, modulo;
float quotient;
// Taking input from user and storing it
// in x and y
cout << "Enter First Number\n";
cin >> x;
cout << "Enter Second Number\n";
cin >> y;
// Adding two numbers
sum = x + y;
// Subtracting two numbers
difference = x - y;
// Multiplying two numbers
product = x * y;
// Dividing two numbers by typecasting one operand to float
quotient = (float)x / y;
// returns remainder of after an integer division
modulo = x % y;
cout << "\nSum = " << sum;
cout << "\nDifference = " <<difference;
cout << "\nMultiplication = " << product;
cout << "\nDivision = " << quotient;
cout << "\nRemainder = " << modulo;
return 0;
}
Output
Enter First Number 8 Enter Second Number 4 Sum = 12 Difference = 4 Multiplication = 32 Division = 2 Remainder = 0
In above program, we first take two numbers as input from user using cin and store it in variable x and y. Then we perform Addition, Subtraction, Multiplication, Division and Modulus on operands x and y and store the result in variable sum, difference, product, quotient and modulo respectively.
Recommended Posts