Chapter 3
Chapter 3
ARITHMETIC IN C
OPERATORS
Operators tell the computer how to
process the data.
It connects data in the expression and
equation
Types of operators used in calculation
and problem solving include
Mathematical
Relational
Logical
OPERATORS CONTINUE.
Operand
Resultant
Example
5 + 7 = 12
Operands are 5 and 7
Operator is +
Resultant is 12
TYPES OF OPERATOR
Mathematical operators
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulo division (%)
Functions
TYPES OF OPERATOR
Relational operators
Equal to (==)
Less than (< )
Greater than (>)
Less than or Equal to (<=)
Greater than or equal to (>=)
TYPES OF OPERATORS
Logical Operator
OR (||)
AND (&&)
Not (!)
EXAMPLE OF OPERATOR
EXPRESSIONS
It is a sequence of operands and operators that reduce to a
single value.
Operator : It is a symbolic token that represents an action to
be
taken.
Ex: * is an multiplication operator.
Operand: An operand receives operators action.
Ex: A+ B In this A and B are operands and + is an operator.
Note: Expressions always evaluate to a single value.
There is no limit to the number of operator and operand sets in
an expression
SETTING UP NUMERICAL
EXPRESSION
Normal equation
X(3Y+4) - 4Y
X+6
Appropriate Computer Equation
X* (3*Y+4)-4*Y/(X+6)
PREPROCESSOR DIRECTIVES
An instruction to pre-processor
Standard library header (p154,Deitel)
E.g. #include <stdio.h>
#include <stdlib.h>
Conversion
#include <string.h>
string processing
TYPES OF OPERATORS
Arithmetic operators
(+ , - , * , / , %)
Relational operators
(> , < , == , >= , <=, !=)
Logical operators (&& , ||)
Compound assignment operator
(+=, -=, *=, /=, %=)
ARITHMETIC OPERATORS
Used to execute mathematical equations
The result is usually assigned to a data storage
(instance/variable) using assignment operator
(=)
C OPERATORS
EXERCISE ON ARITHMETIC
OPERATORS
Given x = 20, y = 3
z=x%y
= 20 % 3
= 2 (remainder)
Reminder:
count -=1;
count--;
--count;
UNARY OPERATORS
Obviously operating on ONE operand
Commonly used unary operators
Increment/decrement { ++ , -- }
Arithmetic Negation { - }
Logical Negation { ! }
END OF CHAPTER