Sei sulla pagina 1di 22

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : T.Sudhakar
Designation : Lecturer in Computer Engg.
Institute : S.S.Govt. Polytechnic, Zaheerabad
Branch : Computer Engg.
Year/Semester : III Semester
Subject : UNIX & C
Code : CM-304
Major Topic : Programming Constructs
Sub-Topic : Arithmetic Operators & Its Evaluation

Duration : 50 Min
Teaching Aids : PPT, Animations
CM304.33 1
Objectives
On completion of this period you would be able to
know
 Operator and its classification
 Arithmetic Operators
 Expression
 Arithmetic Expression
 Evaluation of Arithmetic Expression

CM304.33 2
Recap

1.What is a Variable?

2.What is Constant?

3.What are different Data types in C?

CM304.33 3
Operators
 Symbol that tells the computer to perform
certain mathematical and logical manipulations
Classification of C Operators
 Arithmetic Operators
 Relational Operators
 Logical operators
 Assignment Operators
 Increment and decrement Operators
 Conditional Operators
CM304.33 4
Arithmetic Operators
 These are used for mathematical calculations.
 These can operate on any built in data type.
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
(it gives the remainder after integer division)

CM304.33 5
Examples
Here a and b are variables and are known as
operands
a+b
a–b
a*b
a/b
a%b

CM304.33 6
Integer Arithmetic

 Arithmetic Operation involving only integer


operand.
 % operator can only be used with integer
operands.
If a and b are integers, then for a=14 and b=4

CM304.33 7
Integer Arithmetic

 a – b = 10
 a + b = 18
 a * b = 56
 a/b=3 (Decimal part truncated)
 a%b=2 ( Remainder of division)

CM304.33 8
Modulo division (%)
 Produces remainder of integer division.
 It can be used only on integer type of data.
 The sign of the result is always the sign of
the first operand.
Example:- 14 % -3 = 2
-14 % 3 = -2
-14 %-3 = -2

CM304.33 9
Real Arithmetic
 Arithmetic Operation involves only floating or Real
operand.
 % operator can not be used with real operands.

Examples:-
 If x, y, z are floats then we will have
 x = 6.0 / 7.0 = 0.857143
 y = 1.0 / 3.0 = 0.857143
 z = 3.5 + 6.0 = 9.5
CM304.33 10
Mixed mode Arithmetic
 Arithmetic Operation involves one real operand
and one integer operand.
 In this case, real operation is performed, the
result is always the real number.

Examples:-
 15 / 10.0 = 1.5
 15 / 10 = 1
 15 / 10.0 = 1.5

CM304.33 11
Expression
 Combination of constants, variables and
operators arranged as per the syntax of the
language.

Arithmetic Expression
 Combination of constants and variables
connected by arithmetic operators.

Example: x, x+y, a+b-56

CM304.33 12
Evaluation of Arithmetic Expression

 The expression is scanned from left to right.


 While evaluating an expression the operator
with highest precedence will be evaluated first.
 The operator with least precedence will be
evaluated last.
 The rule that determines which operation is to
be performed first is known as operator
precedence.
CM304.33 13
Precedence of Arithmetic Operators

Operator Order of evaluation (precedence)

Evaluated first. Associativity is left to


()
right
Evaluated second. Associativity is left
*, /, %
to right
Evaluated third. Associativity is left to
+, -
right
Associativity is order in which the operations with
same precedence are carried out.
CM304.33 14
Contd..
3/2*4+3/8+3

1*4+3/8+3 (Integer Division)


4+3/8+3 (Multiplication)
4+0+3 (Integer Division)
4+3 (Addition)
7 (Addition)

CM304.33 15
Contd..
((2 + 3) * 4 – 5 – 6 + 2) / 3 + 4 % 3
(5 * 4 – 5 – 6 + 2) / 3 + 4 % 3

(20 – 5 – 6 + 2) / 3 + 4 % 3

(15 – 6 + 2) / 3 + 4 % 3

(9 + 2) / 3 + 4 % 3

11 / 3 + 4 % 3

3+4%3
3+1
4
CM304.33 16
Summary

In this class, we have discussed about


 Types of Operators in C
 Arithmetic Operators
 Precedence of arithmetic Operators
 Evaluation of Arithmetic Expressions

CM304.33 17
Quiz

1. Assume a = 7, b = 8, c = 15, value of Arithmetic


Expression 3 * ((a % 3) * (7 + (b * 3) / (c – 2)))

a) 28 b) 24

c) 6 d) 9

CM304.33 18
Quiz

1. Assume a = 7, b = 8, c = 15, value of Arithmetic


Expression 3 * ((a % 3) * (7 + (b * 3) / (c – 2)))

a) 28 b) 24

c) 6 d) 9

CM304.33 19
Quiz

2. Assume a = 7.0, b = 2.5, c = 15, value of Arithmetic


Expression 3 * c % 3 + 7 + a / 2 + c – b

a) 28.5 b) 24

c) 6.0 d) 23.0

CM304.33 20
Quiz

2. Assume a = 7.0, b = 2.5, c = 15, value of Arithmetic


Expression 3 * c % 3 + 7 + a / 2 + c – b

a) 28.5 b) 24

c) 6.0 d) 23.0

CM304.33 21
Frequently Asked Questions

1. What is Operator? List different operators in C.

2. List the Arithmetic Operators in C.

3. What is meant by Operator Precedence and


Associativity of Operator?

4. Explain how the Arithmetic Expression is


evaluated.

CM304.33 22

Potrebbero piacerti anche