Simple C++ Expression | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 Answers
+ 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