Evaluating expressions – CSKC
Sign In

Search

Solved Problem #CPP#5358    siteicon   siteicon  

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

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

(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.


Share

Created: 9-Dec-2018 19:47 | Updated: 22-Mar-2026 20:20|