Modulus Operator in C
In the C language, the modulus operator, commonly known as the modulo operator, is a binary arithmetic operator represented by the % (percentile) symbol. Binary operations need two operands, which must be integers because the word "binary" signifies "two." When you are dividing two numbers, the modulo operator is mostly used to determine the remainder.
Example:
int a=11; int b=10; a%b=1
Syntax
Since the modulo operators can only be used with integer variables
a%b
Modulus Operator Return Values in C
The results of an a%b calculation (also known as a percentile b or a modulo b) depend on the values of a and b.
Possible returns for a% b
- Invalid operands to binary% will cause an error when a and b are non-integer numbers.
- If an is not exactly divisible by b, it will return the resultant, or some non-zero number, after dividing a by b.
- If a and b are evenly divisible, a will result in 0.
- If a is an integer and b is 0, a warning for division by zero will appear, and the program will end immediately.
How does the C/C++ modulus operator operate?
The end-user receives two operands for the modulus operator to operate on. The remaining is then calculated by dividing the first and second numbers.
Case: When we divide 7 by 5, which is 7% 5, we get the result one as the quotient and two as the rest of the number when we use the modulus operator to divide between 7 and 5.
Example 1:
#include <stdio.h> int main() { int n1 = 10; int n2 = 4; int res = n1 % n2; printf("%d\n", res); // prints 2 res= n2 % n1; printf("%d\n", res); // prints 4 return 0; }
Output
2 4
Example 2:
#include<stdio.h> void main() { int n1, n2; int res; // variable to store result n1 = 13; n2 = 5; res = n1 % n2; // doing the modulo printf(" The remainder(Modulo) is: %d", res); res = n2 % n1; // define modulus expression printf(" \nThe Modulus operator returns a remainder: %d", res); n1 = 15; n2 = 5; res = n1 % n2; // expression printf(" \nThe Modulus operator returns a remainder: %d", res); }
Output
The remainder(Modulo) is: 3 The Modulus operator returns a remainder: 5 The Modulus operator returns a remainder: 0
Example 3:
#include <stdio.h> #include <stdbool.h> bool isLeap(int years) { if ((years % 4 == 0 && years % 100 != 0) || years % 400 == 0) { // checks the condition and prints result return true; } else { //If not a leap year return false; } } int main() { int years; // printf("Enter the year : "); scanf("%d", &years); // user input int res = isLeap(years); // function calling if (res== true) { //If the res is true, then printf("YES, the given year is leap year"); } else { //If not printf("No, the given year is not leap year"); } return 0; }
Output
Enter the year: 2000 YES, the given year is a leap year
Example 4:
#include <stdio.h> int main() { int n1; // declaration of variable int n2 = 0; // initialize the number n2 to 0 printf("Enter the number : "); scanf("%d", &n1); // taking input int res = n1 % n2; // applying the modulus operator printf("The remainder when n1 is divided by n2 is : %d", res); // error return 0; }
Output
Enter the number: 1 Floating point exception
Example 5:
#include <stdio.h> int main(void) { int x1, y1; int res; x1 = -6; y1 = 9; // modulo operation for negative numbers res = x1 % y1; printf("%d", res); x1 = 9; y1 = -2; res = x1 % y1; printf("\n%d", res); x1 = -5; y1 = -2; res = x1 % y1; printf("\n%d", res); return 0; }
Output
-6 1 -1
Example 6:
#include <stdio.h> int main() { int n1 = 24; int n2 = 7; int n3 = 9; int res = n1 % n2 % n3; printf("%d", res); // prints result return 0; }
Output
3
Generating random numbers using the modulo operator in C
Example:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int numOfIntegers, maxR; // variables declaration printf("Enter the total numbers required: "); scanf("%d", &numOfIntegers); // enter the number of random numbers required printf("Enter the range of max value :"); scanf("%d", &maxR); // Enter the max value of range srand(time(NULL)); for (int i = 0; i < numOfIntegers; i++) { printf("%d, ", rand() % maxR); // creating random numbers in the specified range using the modulus operator } return 0; }
Output
Enter the total numbers required: 5 Enter the range of max value:9 7, 6, 1, 8, 4,