Modulus Operator in C

Modulus Operator in C
Techiio-author
Written by Shuvhojit DebFebruary 9, 2022
9 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, you'll know everything about the modulus operator in C.

Introduction

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.

Syntax

Let’s consider a and b are 2 integers then the modulus expression becomes

a%b

Return value possibilities:

  • If a is not completely divisible by b then it produces some non-zero integer value.
  • If a is completely divisible by b then the remainder becomes 0(zero).
  • If a is some number and b is 0 then we get a compile-time error.

Modulus Operator work in C

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.

Calculation of ‘%’ Operator in C

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

  • a%b >> a-(a/b)*b
  • 8%3 >> 8-(8/3)*3
  • 8-(2)*3
  • 8-6
  • 2

Therefore 8%3 is 2.

Note: The modulus operator always works with integer numbers only.

Implementation of Modulus Operator in C

Below are the examples mentioned:

Example #1

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:

blogpost

Example #2

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:

blogpost

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.

Example #3

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:

blogpost

Explanation: In this example float numerator with an integer denominator will also result in a compile-time error.

Example #4

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:

blogpost

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.

Example #5

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:

blogpost

Conclusion

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.

C language
Programming
Modulus Operator
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance