Just started with c++ and i was hoping someone could tell me how to do these? I'm so confused. The question is "Using the following code, what will x’s be after it passes through the codes?" Thank you!!
1 2 3 4 5 6 7 8 9
int x = 20;
if (x % 2 == 0)
x += 5;
if (x % 5 == 0)
x += 5;
elseif (x % 10 == 0)
x = 10;
else
x = 0;
The modulus operator returns the remainder of two numbers.
1 2 3 4 5 6 7 8 9 10 11
int x = 15;
x % 3
// 15 can be divided evenly by 3
// 15 / 3 == 5 r 0 -> remainder is 0
// thus the answer of this expression is 0
x % 10
// 15 is not evenly divisible by 10
// 15 / 10 == 1 r 5 -> remainder is 5
// therefore this expression will evaluate to 5
From this, we can safely conclude that if a number, x, is evenly divisible by another number, y, x % y will always be 0.
Using the same logic, you should be able to solve it.