Monday, April 1, 2013

Remainder operator (%)

A program showing a few examples of results with the remainder (modulus) operator.


/* The remainder (%) operator */
#include <stdio.h> int main (void) { int a, b, c, d; /* a few operations */ a = 10 % 3; b = -10 % 3; c = 10 % -3; d = -10 % -3; /* you need to double the % to display on screen */ printf ("10 %% 3 is %d\n", a); printf ("-10 %% 3 is %d\n", b); printf ("10 %% -3 is %d\n", c); printf ("-10 %% -3 is %d\n", d); return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
10 % 3 is 1
-10 % 3 is -1
10 % -3 is 1
-10 % -3 is -1

No comments:

Post a Comment