Cracking more codes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

14th Jun 2017, 8:53 PM
21kHzBANK21kHZ
21kHzBANK21kHZ - avatar
5 Answers
+ 5
Remainder concern integer division, where result is necessarly an integer... So, 1/2 = 0, rest 1 ( 2x0+1 == 1 ) ^^
14th Jun 2017, 8:56 PM
visph
visph - avatar
+ 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.
14th Jun 2017, 9:30 PM
21kHzBANK21kHZ
21kHzBANK21kHZ - avatar
+ 2
man, that is confusing I guess I will have to reverse engineer my thinking. thanks visph
14th Jun 2017, 9:09 PM
21kHzBANK21kHZ
21kHzBANK21kHZ - avatar
- 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)
14th Jun 2017, 9:27 PM
Milind
Milind - avatar
- 1
in "integer​" maths num1 % num2 returns the remainder which is 1 (not 0) and the quotient is 0 (not 0.5)
14th Jun 2017, 10:25 PM
Milind
Milind - avatar