C - Get the remainder with modulus operator

Introduction

% is the modulus operator. It calculates the remainder of one number divided by another.

Demo

#include <stdio.h> 

#define VALUE 5 //  www  .  j av a2s . c  om

int main() 
{ 
   int a; 

   printf("Modulus %d:\n",VALUE); 
   for(a=0;a<30;a++) 
       printf("%d %% %d = %d\n",a,VALUE,a%VALUE); 
   return(0); 
}

Result

The %% placeholder merely displays the % character.

A modulus operation displays the remainder of the first value divided by the second. So 20 % 5 is 0, but 21 % 5 is 1.

Related Topic