There are 5 kinds of arithmetic operators in C++ -
+ operator(addition).
- operator(subtraction).
* operator(multiplication).
/ operator(division).
% operator(modulus).
Arithmetic operators(+, -, *) operating on primitive data types.
// Arithmetic Operator Example in C
#include<iostream>
using namespace std;
int main()
{
int x=10, y=10; // local variables
int sum = x + y; //addition of two local int variables
int difference = x-y; //subtraction of two local int variables
int multiplication = x * y; //multiplication of two local int variables
cout<<"Sum of two int variables = "<< sum << "\n";
cout<<"Subtraction of two int variables = " << difference <lt; "\n";
cout<<"Multplication of two int variables = " << multiplication << "\n";
return 0;
}
Output is
Sum of two int variables = 20
Subtraction of two int variables = 0
Multplication of two int variables = 100
Division operator / and Modulus operator %
Division operator / gives us the resulting quotient value after dividing two int values.
Modulus operator % gives us the remainder value after dividing two int values.
/* Division and Modulus operator in C */
#include<stdio.h>
int main()
{
int i=40;
int j=10;
int quotient = i/j;
int remainder = i%j;
printf("Quotient after dividing 40 by 10 : %d \n", quotient);
printf("Remainder after diviiding 40 by 10 : %d \n", remainder);
int a = 70;
int b = 20;
quotient = a/b;
remainder = a%b;
printf("Quotient after dividing 70 by 20 : %d \n", quotient);
printf("Remainder after dividing 70 by 20 : %d", remainder);
return 0;
}
Output-
Quotient after dividing 40 by 10 : 4
Remainder after diviiding 40 by 10 : 0
Quotient after dividing 70 by 20 : 3
Remainder after dividing 70 by 20 : 10
Advertisement
Priorities of arithmetic operators in arithmetic expressions.
+ and - operator have a least priority in an arithmetic expression.
*, / and % operators have an equal highest priority in an arithmetic expression.
Calculating arithmetic expressions with equal and unequal priorities.
Calculation of an arithmetic expression involving operators with unequal priorities starts with the calculation of
a sub-expression that involves an operator with the highest priority.
Calculation of an arithmetic expression involving operators with equal priorities is made by evaluating expressions from left to right.
For example, when calculating an arithmetic expression 3+2x4-2, sub-expression involving * operator will be calculated first,
because operator * has highest priority. Hence -
3+2*4-2, first 2*4 will be calculated, which results in the next expression
3+8-2, now + and - operator have an equal priority, so, expression will be calculated from left to right
11-2, which results in
9
Let's understand with a code -:
/* Arithmetic expressions with equal and unequal priority operators */
#include<stdio.h>
int main()
{
int result = 3+2-4+2; /* + - operators have equal priorities */
printf("Result of 3+2-1+2 is : %d \n", result);
result = 3+2*4+2; /* + * have unequal priorities */
printf("Result of 3+2*4+2 is : %d \n", result);
result = 3+2*4/2; /* + * / operators have unequal priorities */
printf("Result of 3+2*4/2 is : %d \n", result);
result = 3%2*4/2; /* % * / operators have equal priorities */
printf("Result of 3%2*4/2 is : %d", result);
return 0;
}
Output-
Result of 3+2-1+2 is : 3
Result of 3+2*4+2 is : 13
Result of 3+2*4/2 is : 7
Result of 3*4/2 is : 2
Program Analysis
First arithmetic expression 3+2-4+2, is calculated left to the right as + - operator have equal priorities.
Second expression 3+2*4+2, * operator have more priority than + , hence 2*4 is calculated first.
Third expression 3+2*4/2, * / operators have more priority than + , hence 2*4 is calculated first
Fourth expression 3%2*4/2, % * / operators have an equal priority, hence expression is calculated from left to the right, starting with 3%2.
Arithmetic expressions with sub-expressions in parenthesis ( ) will always be executed first
#include<stdio.h>
int main()
{
int result = (3+2)*4+2;
printf("Result of (3+2)*4+2 is : %d \n", result);
result = 3%(2*4)/2;
printf("Result of 3%(2*4)/2 is : %d ", result);
return 0;
}
Output-
Result of (3+2)*4+2 is : 22
Result of 3(2*4)/2 is : 1
Program Analysis
First expression (3+2)*4+2, although * has more priority than + but the sub-expression in () has the most priority hence (3+2) is calculated first.
Second expression 3%(2*4)/2, although * % / have equal priority so this expression could be calculated left to right but
the sub-expression in () has most priority hence (2*4) is calculated first.