C program for using modulo operator, finding if number is dividable and print remainder.

The remainder operator (otherwise known as the modulo operator) % is a binary operator (i.e., takes exactly 2 operands) and operates only on integer types (e.g., short, int, long, long long, etc).

 $ vim using_mod.c 
#include <stdio.h>

int main(void) {
        float num = 6.00;
        int remainder = (int)num % (int)3.0;

        if (remainder == 0) {
                printf("number is divisible\n");
        } else {
                printf("number is not divisible: Remainder = %d\n", remainder);
        return 0;
 $ gcc -o using_mod using_mod.c 
 $ ./using_mod
number is divisible

Now, if we change the number from 6 to something else, for example 11, the output will be as below,

 $ ./using_mod
number is not divisible: Remainder = 2

Leave a Comment