+ 3
Cracking more codes
I'm stuck trying to understand a code here: #include <iostream> using namespace std; int main(){ int x=5; int y=2; for (int n=1; n<=x;n++){ if(n%y==0){ continue; } else { cout<<n; } } } Specifically, I'm stuck trying to figure out how 1%2 is 1? There is no remainder when dividing 1 by 2, the answer is 0.5 remainder nothing and 2 * 0.5 is perfectly 1. So what gives?
5 Answers
+ 5
Remainder concern integer division, where result is necessarly an integer... So, 1/2 = 0, rest 1 ( 2x0+1 == 1 ) ^^
+ 3
Wonder yourself; the remainder in this problem is not the answer that C++ returns. if you went to school you would know that the remainder of 1 divided by 2 is not 1.
+ 2
man, that is confusing I guess I will have to reverse engineer my thinking. thanks visph
- 1
I am wondering if you went to school. this is basic maths.
When you divide one number with another number, you get quotient and remainder.
eg.
num1 / num2 is well-known to return quotient
num1 % num2 returns the remainder. (in usually integerâ maths)
- 1
in "integerâ" maths
num1 % num2 returns the remainder which is 1 (not 0)
and the quotient is 0 (not 0.5)