Full Stack Developer
The modulus driver in C is denoted by (percentile) driver. This modulus driver added to computation drivers. This modulus driver works in between 2 operands. The modulus driver finds the division with numerator by denominator which results in the remainder of the number. Remainder always integer numbers only. However, also it gives you 0 (zero) as the remainder If no remainder is there.
Let’s consider a and b are 2 integers then the modulus expression becomes
a%b
Return value possibilities:
Modulus driver works grounded on the value entered by the end- stoner. It always finds the remainder of the 2 figures concerning the numerator.
The below illustration will illustrate the exact functionality.
Illustration: 7%3 gives us remainder as 1 because when we divide 7 by 3 also we get 2 as quotient and 1 as remainder.
Same way: 8%3 gives us remainder as 2 because when we divide 8 by 3 also we get 2 as quotient and 2 as remainder.
Let’s see the internal calculation of the ‘%’ operator in C:
a%b will be resolved as a-(a/b)*b
Example:
Let a=8 and b=3, then
Therefore 8%3 is 2.
Note: The modulus operator always works with integer numbers only.
Below are the examples mentioned:
Remainder for integer numbers
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
int a, b;
//declare one more variables for store result
int output;
//Asking user to enter integer input
printf("Please enter any 2 integer numbers \n");
scanf("%d\n%d",&a,&b);
//Finding the remainder with modulus operator
output = a % b;
//displaying output to the end user
printf("Remainder of %d and %d is = %d", a,b,output);
return 0;
}
Output:
Remainder with float numbers
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
float a, b;
//declare one more variables for store result
float output;
//Asking user to enter integer input
printf("Please enter any 2 integer numbers \n");
scanf("%f\n%f",&a,&b);
//Finding the remainder with modulus operator
output = a % b;
//displaying output to the end user
printf("Remainder of %f and %f is = %f", a,b,output);
return 0;
}
Output:
Explanation: As we discussed in this example we are trying to find out the remainder for 2 float numbers that result in a compile-time error.
Remainder for numerator float and denominator int
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
float a;
int b;
//declare one more variables for store result
int output;
//Asking user to enter integer input
printf("Please enter any 2 integer numbers \n");
scanf("%f\n%d",&a,&b);
//Finding the remainder with modulus operator
output = a % b;
//displaying output to the end user
printf("Remainder of %f and %d is = %d", a,b,output);
return 0;
}
Output:
Explanation: In this example float numerator with an integer denominator will also result in a compile-time error.
Remainder for numerator int and denominator float
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
int a;
float b;
//declare one more variables for store result
int output;
//Asking user to enter integer input
printf("Please enter any 2 integer numbers \n");
scanf("%d\n%f",&a,&b);
//Finding the remainder with modulus operator
output = a % b;
//displaying output to the end user
printf("Remainder of %d and %f is = %d", a,b,output);
return 0;
}
Output:
Explanation: In this example, int numerator with float denominator will also result in a compile-time error. This concludes both values must be integer type only.
Remainder with zero denominators
Code:
//include is used to add basic C libraries
#include <stdio.h>
//main method is used to run C application
int main(void)
{
//declaring 2 variables
int a;
int b=0;
//declare one more variables for store result
int output;
//Asking user to enter integer input
printf("Please enter any 1 integer number \n");
scanf("%d",&a);
//Finding the remainder with modulus operator
//denominator 0 will result into undefined so we got exception in the output
output = a % b;
//displaying output to the end user
printf("Remainder of %d and %d is = %d", a,b,output);
return 0;
}
Output:
C modulus driver is used to find the remainder of the 2 figures. This is always integer only. An important conclusion from the below illustration is modulus driver is applicable only on integer figures.