how is it calculeting..??

Greetings everyone...

i would like to know how c++ is calculating this equations manually...??
if i put this codes in c++ compiler i got those answers.. but how..?? i cant use compiler for theory exam.. so please help me..

i also would like to know how does this "%" sign works... how is it calculating in "question1 and question 2"..??

does c++ also follows "BODMAS" rule...??

1
2
3
4
5
6
7
8
9

//question 1
cout<<(7 % 5 = 2); // error... "non-lvalue in assignment" 

//question 2
cout<<3 + 5 % 10 * 6;  // answer : 33

//question3
cout<<5 * 5/ 10 * 10 / 5; //answer:4 
Look at this page: http://en.cppreference.com/w/cpp/language/operator_precedence

Example: 2 * 5 + 1 - 2

* has the highest precedence so it is 2 and 5 that will be multiplied: (2 * 5) + 1 - 2

+ and - both has the same precedence so instead we look at the associativity column on the right. It says "Right-to-left" "Left-to-Right" which gives: ((2 * 5) + (1 - 2)) (((2 * 5) + 1) - 2)

EDIT: Fixed mistake pointed out by keskiverto below.
Last edited on
I was wondering, is the first one supposed to have a deliberate error, or was it merely mistyped?
1
2
// question 1
cout<<(7 % 5 == 2); // maybe == not = 
thanks for the reply @Peter87.. well how if divide(/) and multiply(*)..?? which works first as in the case of question 3

thanks @Chervil... tat works... :D
Your question3 contains integer math. Integer division discards remainder.

Peter87 wrote "Right-to-left", probably as typo, for to me the table looks like "Left-to-right". Therefore,
1
2
3
4
5
6
7
8
9
10
11
5 * 5 / 10 * 10 / 5
==
(((5*5)/10)*10)/5
==
((25/10)*10)/5
==
(2*10)/5
==
20/5
==
4
keskiverto wrote:
Peter87 wrote "Right-to-left", probably as typo, for to me the table looks like "Left-to-right".

Yeah, my mistake. I accidentally looked at the unary operators.
Topic archived. No new replies allowed.