The Modulus Operator
The last arithmetic operator to be presented in this chapter is the modulus operator, which is symbolized by the percent sign (%). Try to determine how this operator works by analyzing the output from Program 4.4.
Program 4.4.
// The modulus operator#import <Foundation/Foundation.h>int main (int argc, char * argv[]){ @autoreleasepool { int a = 25, b = 5, c = 10, d = 7; NSLog (@"a %% b = %i", a % b); NSLog (@"a %% c = %i", a % c); NSLog (@"a %% d = %i", a % d); NSLog (@"a / d * d + a %% d = %i", a / d * d + a % d); } return 0;}
Program 4.4. Output
a % b = 0a % c = 5a % d = 4a / d * d + a % d = 25
Note the statement inside main that defines and initializes the variables a, b, c, and d in a ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access