Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a percentage sign (%) do mathematically in Objective C?

Tags:

objective-c

I am super confused what the percentage sign does in Objective C. Can someone explain to me in language that an average idiot like myself can understand?! Thanks.

like image 270
Aero Wang Avatar asked Dec 20 '22 16:12

Aero Wang


1 Answers

% is the modulo operator, so for example 10 % 3 would result in 1.

If you have some numbers a and b, a % b gives you just the remainder of a divided by b.

So in the example 10 % 3, 10 divided by 3 is 3 with remainder 1, so the answer is 1.

If there is no remainder to a divided by b, the answer is zero, so for example, 4 % 2 = 0.

Here's a relevant SO question about modular arithmetic.

like image 63
Charles Martin Avatar answered Jun 02 '23 16:06

Charles Martin