0

Simple C++ Expression

// I know that this one is simple, but I’m only about 25% into the C++ course. Can someone explain the answer, please? Thank you. “What is the result of the following expression?” cout << 1000/100%7*2; // My logic: Using PEMDAS (I’m not sure when to execute modulus), 7*2 = 14 1000/100 = 10 10%14 = 10 output = 10 // Alt logic: 100%7 = 2 2*2 = 4 1000/4 = 250 Output = 250

5th Mar 2021, 8:27 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
3 odpowiedzi
+ 3
https://en.cppreference.com/w/cpp/language/operator_precedence Says it executes left to right for operators /, *, % . So most probably it's executing that way only like "/" first , then "%" and after we have "*".
5th Mar 2021, 8:40 PM
Abhay
Abhay - avatar
+ 3
C++ logic: from left to right 1000/100=10 10%7=3 3*2=6 There are no brackets in your expression. Division, multiplication, and modulus have equal status. It means that your expression is executed from left to right.
5th Mar 2021, 8:42 PM
Katty Leony
+ 1
Abhay and Katty Leony , Thank you. I’ll review that link.
5th Mar 2021, 8:54 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar