+ 1
Help with %
I started learning c# recently and I'm having a hard time understanding what % actually does, and if it has multiple uses. https://code.sololearn.com/c2ibSDDlzqSa/?ref=app
4 ответов
+ 4
% is getting remainder of division
For example
9 = 4(2)+1 so 9%2 returns 1
18 = 3(5) + 3 so 18%5 returns 3
A special case of remainder is 0,
n%i will be 0 when n is divisible by 0.
So there will be two extensions of usage:
The first one is in your example:
The i%2==0 is commonly used to check if i is an even number because an even number is divisible by 2 and has no remainder, hence i%2==0 will return true
Another usage is, when used together with for loop, for checking whether a number is prime.
+ 1
Thank you guys foe the answers <3
+ 1
9%2=1
Why 1 ?
-------------
9/2 = 4 and remind 1 <<it is %
Or
4*2 = 8 next> 8+1=9