Tutorial 04 - Expressions and Operators

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

AACS1074 Programming Concepts and Design I Tutorial 4

Tutorial 4: (Expressions and Operators)


1. Suppose a, b and c are integer variables that have been assigned the values
a = 8, b = 3 and c = - 5. Determine the value of each of the following arithmetic
expression.

Expression Value Expression Value


a) a + b + c f) a % c
b) 2 * b + 3 * (a – c) g) a * b / c
c) a / b h) a * (b / c)
d) a % b i) (a * c) % b
e) a / c j) a * (c % b)

2. A C program contains the following declarations:

int i, j;
long l;
short s;
float f;
double d;
char c;

Determine the data type of each of the following expressions.

Expression Data Type Expression Data Type


a) i + c f) s + j
b) f + c g) l + j
c) d + f h) s + c
d) ((int)d) + l i) l + c
e) i + f j) ((float)i) + c

3. Given int a = 1, b = 10; What is the output produced by the following code segment?

Statement Output
printf("a = %d, b = %d\n", a++, b++);
printf("a = %d, b = %d\n", ++a, ++b);
printf("a = %d, b = %d\n", --a, b--);
printf("a = %d, b = %d\n", a--, b--);

4. Convert the following mathematical equations into C statements (treat each letter as
an individual variable):

a) p= 2(w+h)

b) a
s= (xy-mn)
b

c) m+n
u=
x-y

d) a(b+c)
t= +3xy
1-mn

Page 1 of 2
AACS1074 Programming Concepts and Design I Tutorial 4

5 . (a) The condition for passing a test is: mark >= 50


To get the condition for failing the test, negate the above expression using:
 The ! operator
 DeMorgan's theorem

(b) Negate the following condition (for invalid mark) using DeMorgan's Theorem:
mark < 0 || mark > 100

6. A C program contains the following declarations and initial assignments:

int i = 8, j = 5;
double x = 0.005, y = -0.01;
char c = 'c', d = 'd';

Determine the value of each of the following expressions. Use the value initially
assigned to the variables for each expressions.

NOTE: 1) ASCII value for ‘c’ is 99 and ‘d’ is 100.


2) Boolean results: TRUE = 1, FALSE =0; value 0 is FALSE, non-0 is TRUE

Expression Value
a) (3 * i – 2 * j) % (2 * d – c)
b) 2 * ((i / 5) + (4 * (j – 3)) % (i + j – 2))
c) -(i + j)
d) ++i
e) i<=j
f) c>d
g) X >=0
h) j != 6
i) 5 * (i + j) > 'c'
j) 2 * x + y == 0
k) !(i <= j)
l) !(c == 99)
m) !(x > 0)
n) (j < 5) && (i > 0)
o) (i > 0) || (j < 5)
p) (x > y) && (i > 0) || (j < 5)

Page 2 of 2

You might also like