Professional Documents
Culture Documents
ARITHMETIC OPERATIONS
TYPE CONVERSION
+ Addition 4+2 6
- Subtraction 4-2 2
* Multiplication 4*2 8
/ Division 4/2 2
Almost all of the program use some sort of data stored in variables
In a program different types of operations are performed on that data
The data on which operations are performed are knows as operands and
the types of the operations performed are known as operators
Example:
Operator
A+B
Operands
ORDER OF PRECEDENCE
a.
b.
c.
ORDER OF PRECEDENCE (CONT.)
a. (27/5)+(15*8)-15
b. 13+(2*16)/3-18
c.
(3/4)-3+(15/5)3
ORDER OF PRECEDENCE (CONT.)
+= Addition Assignment a += b a = a + b
-= Subtraction Assignment a -= b a = a - b
/= Division Assignment a /= b a = a / b
*= Multiplication Assignment a *= b a = a * b
%= Modulus Assignment a %= b a = a % b
TRACE THE OUTPUT BELOW:
Example 2:
Example 1:
int a=38;
int b=2;
int a=29;
a*=b;
int b=34;
b=a;
a+=b;
cout << a << endl;
cout << a;
cout << b;
TRACE THE OUTPUT BELOW:
Example 2:
Example 1:
int a=38;
int b=2;
int a=29;
a*=b;
int b=34; 63 76
b=a; 76
a+=b;
cout << a << endl;
cout << a;
cout << b;
VARIATION OF ASSIGNMENT STATEMENT
(CONT.)
Example:
Expression Equivalent to
price = price * rate; price *= rate;
count = count + 3; count += 3;
amount = amount / 2; amount /= 2;
price *= rate + 1; ??
VARIATION OF ASSIGNMENT STATEMENT
(CONT.)
Example:
Expression Equivalent to
price = price * rate; price *= rate;
count = count + 3; count += 3;
amount = amount / 2; amount /= 2;
price *= rate + 1; ??
int a=25;
int b=2;
a*=b+3;
cout << a << endl;
EXAMPLE
int a=25;
int b=2;
a*=b+3;
125
cout << a << endl;
ASSIGNMENT STATEMENT
For a variable to increase or decrease by 1, C++ provides two unary
operators:
➢ ( ++ ) : increment operator
➢ ( -- ) : decrement operator
Example:
ASSIGNMENT STATEMENT (CONT.)
➢ Pow (x,y)
sqrt (x)
➢
sqrt (x)
➢
MATHEMATICAL LIBRARY FUNCTIONS (CONT.)