Please help for this equation in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please help for this equation in c++

Input (7%6)/2*5 output: 0 how? Please answer...

18th Jul 2019, 5:32 AM
Mayank Kumar
Mayank Kumar - avatar
7 Answers
+ 8
S.k. techno point ok, anyway, maybe, the question is not complete
18th Jul 2019, 6:32 AM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 7
1. 7%6 is remainder when dividing 7 to 6 that is 1 2. 1/2=0.5 3. 0.5*5=2.5 It seems like your output must be an integer so 2.5 becomes 2 I hope it was useful:)
18th Jul 2019, 6:11 AM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 3
S.k. techno point Oh, in your question I thought you said the output is 2. In my answer the note at the bottom is wrong I guess. When it divides 1 by 2 in integer division, it would return 0 and then 0 * anything is 0.
18th Jul 2019, 5:58 AM
JS Coder
+ 2
By the order of operations, parentheses come first, so the equation is now 1/2*5 (7%6 = 1 remainder 1, mod is the remainder, so returns 1). Since the operators are now at the same level, they are executed left to right. First 1/2, which is 0.5, then 0.5*5, which is 2.5. Now since this is integer division (there are no floats or dou les in the original inputted expression) the decimal is discarded, leaving 2. Note: the decimal is only removed at the end, so 0.5 isn't turned to 0 mid-evaluation causing 0 as the answer.
18th Jul 2019, 5:41 AM
JS Coder
+ 1
But the answer comes out to be 0 when I run it.
18th Jul 2019, 5:52 AM
Mayank Kumar
Mayank Kumar - avatar
+ 1
Ook... thanks...
18th Jul 2019, 6:00 AM
Mayank Kumar
Mayank Kumar - avatar
+ 1
Zhenis Otarbay there was an error in the question kindly check it again please...
18th Jul 2019, 6:28 AM
Mayank Kumar
Mayank Kumar - avatar