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 ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.