Example(s):Palindrome, prime, armstrong, "linear search", reverse etc.

Example(s):1575, 1632, 1539 (Only one at a time)

Login

[lwa]

Solved Problem
#CPP#5358

Problem Statement - Evaluating expressions

What will be the value of c after evaluating the given expressions on the left side for following two set of statements. Also justify your answer

(i) int j=5; int c=(5*++j)%6;
(ii) int j=5; int c=(5*j++)%6;

Solution

TC++ #5358

Run Output

0
1

(i) int j=5; int c=(5*++j)%6;
this will evaluate as follows
(5*6)%6 //prefix increment will make j as 6
(30)%6 //Modulus operator will calculate remainder of division 30/6
0 //Remainder will be 0

(ii) int j=5; int c=(5*j++)%6;
this will evaluate as follows
(5*5)%6 //postfix increment will not increment j in this instruction
(25)%6 //Modulus operator will calculate remainder of division 25/6
1 //Remainder will be 1

Common Errors

Student often confuse between prefix and postfix operators. Just remember that in prefix operator computation happens after incrementing and in postfix operator computation happens and then incrementing is done.