Who can explain me this? C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Who can explain me this? C++

I wrote this code to find the pair numbers into de 1 to 25. I wrote well the code but I want to know the symbol "%" how works into the "If". https://code.sololearn.com/cjcG7Sp3YyM6/?ref=app

19th Apr 2020, 5:16 PM
Diego Fernandez
Diego Fernandez - avatar
12 Answers
+ 2
sp you made for loop from 1 to 25(incl) all even numbers modulus by 2 equals 0(dividet by 2 an got reminder 0) and we got 2,4,6.....24
19th Apr 2020, 5:27 PM
george
george - avatar
+ 3
'%' gives remainder after division ,So here when n%2 means it gives remainder after dividing with 2 .As it is Number 2 it just divides numbers and yields 0 or 1 as reminder .When it's even number it gives remainder 0 therefore, 0 is equal to 0 it becomes true and evaluated to be true and prints Number.
20th Apr 2020, 5:10 PM
Nikhil Maroju
Nikhil Maroju - avatar
+ 2
Vivek all numbers ar divisible by 2 3/2 is 1.5 so it is not correct explanation. Division by two WITHOUT REMINDER gives us even numbers.
19th Apr 2020, 7:19 PM
george
george - avatar
+ 2
divisble in the sense means it divides the whole no
19th Apr 2020, 7:26 PM
VIVEK NIRMALKAR
VIVEK NIRMALKAR - avatar
+ 1
ok my friend % is division by modulus. you know that when you divide even numner to 2 you always got number without the reminder.
19th Apr 2020, 5:25 PM
george
george - avatar
+ 1
when it goes to if section It checks the condition that number%2==0 or not means suppose your number is 4 then it checks 4%2==0 or not so 4%2=0 % just used to get remainder. 4/2 when you divide this then u get 0. so in this way the number you get on the output screen all are divisible by 2 or you can say all are even number.
19th Apr 2020, 7:11 PM
VIVEK NIRMALKAR
VIVEK NIRMALKAR - avatar
+ 1
like 4/2 remaimder 0 but 3/2 remainder 1.5
19th Apr 2020, 7:26 PM
VIVEK NIRMALKAR
VIVEK NIRMALKAR - avatar
+ 1
You are wrong. 2/4=0.5 3/2=1.5 in case of int there are 0 and 1 but in case of float or double there are 0.5 and 1.5 corresponding. so Division by 2 without remainder it is not the same as division by 2.
19th Apr 2020, 7:27 PM
george
george - avatar
+ 1
sryy 4/2
19th Apr 2020, 7:30 PM
VIVEK NIRMALKAR
VIVEK NIRMALKAR - avatar
+ 1
another example 25%24 gives u 1 in int. 25/24 remqinder 1
19th Apr 2020, 7:34 PM
VIVEK NIRMALKAR
VIVEK NIRMALKAR - avatar
+ 1
Only in case of int!!!!. But division is /, modulus or division by modulus is %. So never use assign int after division except if it really necessary.
19th Apr 2020, 7:38 PM
george
george - avatar
0
george VIVEK NIRMALKAR Thank you so much!
19th Apr 2020, 7:52 PM
Diego Fernandez
Diego Fernandez - avatar