What is the output of (X%Y) ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

What is the output of (X%Y) ?

I had an easy code in a challenge: x=7; while(x%3) { x++; } cout << x; My answer was "7", because I thought "seven don't divide by three, so (x%3)= 'false' and loop is not used". It's wrong answer, but pls explain me why 🙏. I was looking for post about it, but didn't find it, so it would be great if u showed me explanation.

21st May 2018, 8:19 AM
Łukasz Sidor
Łukasz Sidor - avatar
11 Answers
+ 7
x%3 does not evaluate to true or false in this sense. It outputs values from 0 to 2 (remainder). 7%3 = 1, since you can divide 6 by 3 without rest, i.e. 1 is the remainder. Every number different from 0 will be transformed into "true". So only if the number is divisable by 3, the expression turns to 0, i.e. "false" 8%3 = 2 9%3 = 0 Therefore the result is 9.
21st May 2018, 8:33 AM
Matthias
Matthias - avatar
+ 23
if x % 3 result in sth rather than zero, it translate to true for the while loop. so the loop continues. x = 7, 7%3 = 1 x = 8, 8%3 = 2 x = 9, 9%3 = 0
21st May 2018, 8:37 AM
Amir
Amir - avatar
+ 3
thanks alot for you . I answer the program without matrix. https://code.sololearn.com/cSljMzPPrqir/?ref=app
25th May 2018, 1:57 PM
‎سلطان سعيد‎
‎سلطان سعيد‎ - avatar
+ 3
7%3!=0 so it is true and it will stop when i=9 because 9%3==0
26th May 2018, 4:46 PM
Dilshodjon Olimov
Dilshodjon Olimov - avatar
+ 2
I always knew what modulus is but this was my first time to encounter it in such a way. Thanks for explanation :)
21st May 2018, 8:37 AM
Łukasz Sidor
Łukasz Sidor - avatar
+ 2
please I need the code of program inter the number in the decimal and out put of binary number in c++
24th May 2018, 11:22 PM
‎سلطان سعيد‎
‎سلطان سعيد‎ - avatar
+ 2
Remainder!
25th May 2018, 6:17 AM
Gummadi Nishna
Gummadi Nishna - avatar
+ 2
Modulus is like division. 7 is divided by 3 then it is the REMAINDER that counts. How many times can 3 go into 7? 6. What is left to reach 7? 1. That is how to find answer for all modulus problems. You can check by adding the remainder and multiple(the number used to reach the other) together so that they add and are the original number being divided into.
25th May 2018, 8:23 AM
Apple Blossom
Apple Blossom - avatar
+ 1
the remainder of the division is the correct answer like: 10%4==2 and the condition here while ( the result of x%3 !=0)
24th May 2018, 4:19 PM
B.M
B.M - avatar
0
Sorry, I can't write your name :# I used to think there is a single-line-function for conversion but I didn't find it, but there is simple code for it: https://www.javatpoint.com/cpp-program-to-convert-decimal-to-binary I'm surprised Solo Learn doesn't have any thread about that. It's easy and useful. It was one of the first thing I was taught in school.
25th May 2018, 6:54 AM
Łukasz Sidor
Łukasz Sidor - avatar
0
Łukasz Sidor سلطان سعيد There are many. Just search "decimal binary c++" in Q&A in the section "most popular" There is also a challenge in the learning section.
25th May 2018, 9:12 AM
Matthias
Matthias - avatar