You are on page 1of 24

TOPIC 2 (PART II):

ARITHMETIC OPERATIONS
TYPE CONVERSION

 Type conversion or typecasting refers to changing an entity of one


datatype into another
 It is used in computer programming to ensure variables are correctly
processed by a function.
 Expressed using static_cast keyword as follows :

static_cast <data_type> (expression)


TYPE CONVERSION (CONT.)
ARITHMETIC OPERATORS

Operator Definition Example Output

+ Addition 4+2 6

- Subtraction 4-2 2

* Multiplication 4*2 8

/ Division 4/2 2

% Modulus (Remainder) 5%2 1


ARITHMETIC OPERATIONS (CONT.)

 +, -, * and / operation can be performed on integer numbers as well as


floating numbers
 Remainder operation can only be performed on integer numbers
 Floating point operands are not accepted by remainder operator
 Unary operator – has only one operand
 Binary operator – has two operand
OPERATORS AND OPERANDS

 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

 All operations inside of ( ) are evaluate

 *, /, and % are at the same level of precedence and are evaluated


next
 + and – have the same level of precedence and are evaluated last

 When operators are on the same level


➢ Performed from left to right
ORDER OF PRECEDENCE (CONT.)

 Exercises: Rewrite the following statements in C++ format.

a.

b.

c.
ORDER OF PRECEDENCE (CONT.)

 Exercises: Rewrite the following statements in C++ format.

a. (27/5)+(15*8)-15

b. 13+(2*16)/3-18

c.
(3/4)-3+(15/5)3
ORDER OF PRECEDENCE (CONT.)

 Given the following variable declarations:


float x = 1.5;
int y = 3, z = 7;

 Determine the output of the following statement:


➢ int t = y * z + z * (6 – y);
cout << t;
➢ int u = (y + z) % 3 + 12 / y + 6;
cout << u;
➢ float v = z / x + 1;
cout << v;
ORDER OF PRECEDENCE (CONT.)

 Given the following variable declarations:


float x = 1.5;
int y = 3, z = 7;

 Determine the output of the following statement:


➢ int t = y * z + z * (6 – y);
cout << t; 42
➢ int u = (y + z) % 3 + 12 / y + 6;
cout << u; 11
➢ float v = z / x + 1;
cout << v; 5.6667
VARIATION OF ASSIGNMENT STATEMENT

Operation Description Example Equivalent to

+= 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; ??

Price = price * (rate +1)


EXAMPLE

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

Prefix ++i; Increment the value of i (i = i + 1), then use it

--i; Decrement the value of i (i = i – 1), then use it

Suffix i++; Use the value of i, then increment (i = i + 1) it

i--; Use the value of i, then decrement (i = i - 1) it


ASSIGNMENT STATEMENT (CONT.)

 Example:
ASSIGNMENT STATEMENT (CONT.)

++i; Increment the value of i (i = i + 1), then use it


--i; Decrement the value of i (i = i – 1), then use it
i++; Use the value of i, then increment (i = i + 1) it

 Exercises i--; Use the value of i, then decrement (i = i - 1) it

Expression Value X Value Y


x = 6;
y = ++x;
x=10;
y=x++;
x = 5;
y = --x;
x = 10;
y = x--;
MATHEMATICAL LIBRARY FUNCTIONS

 Perform compute common mathematical operations and transformations


➢ Include the header file <cmath>
 Example :
MATHEMATICAL LIBRARY FUNCTIONS (CONT.)

 Exercises: Write C++ assignment for the following equations.

➢ Pow (x,y)

sqrt (x)

sqrt (x)

MATHEMATICAL LIBRARY FUNCTIONS (CONT.)

 Exercises: Write C++ assignment for the following equations.

➢ Pow (x,y) a=4*pow((x-3),2)

sqrt (x) b=sqrt(pow(x,2)+(3/4))


sqrt (x) c=sqrt(t-pow(r,3))+(4/t)


You might also like