Modulus Operator in C | Depth Concepts of Modulus Operator | Hidden Concepts of C Operators

Spread the love
Modulus operator in C languae
Modulus Operator behavior is very Confusing one for beginners it have different behavior. Now we are going to discuss about Modulus Operators and it’s behavior with Examples.. Hope you will enjoy this..

What is Modulus Operator : 

  • Modulus Operator gives the Remainder of of division of one number by another. 
Example :
If we have two numbers 11 and 2, then if we apply modulus operator on it,. the expression “11 mod 2” would evaluate to 1 because 11 divided by 2 leaves a quotient of 5 and a remainder of 1. 
   a = 11 % 2 ; =======> a = 1;
Program :
  1. int main()
  2. {
  3.         int a;
  4.         a = 11 % 2;
  5.         printf(“%d n,a);
  6.         return 0;
  7. }
Output :

1

Modulus Operator Output Sign :

  • Modulus Operator output sign always Depends on the Numerator only. 
Example :
  1. #include<stdio.h>
  2. int main()
  3. {
  4.         int a;
  5.         printf(“a = %d n,11 % 2);
  6.         printf(“a =%d n, 11 % 2);
  7.         printf(“a = %d n, 11 % 2);
  8.         printf(“a = %d n, 11 % 2);
  9.         return 0;
  10. }
Output :

a  =   1
a  =  -1
a  =   1
a  =  -1

Form Above Example that is clear Modulus Operator Sign is always Depends on Numerator Sign Only..

Numerator Value :

  • When the Numerator Value is less than the denominator Value, then output will be Numerator Value.
Example :
  1. int main()
  2. {
  3.         int a;
  4.         printf(” a = %dn, 1 % 5);
  5.         printf(” a = %dn, 5 % 10);
  6.         return 0;
  7. }
Output :

a = 1
a = 5

Floating point data is Not Allowed :

  • When we are working with MOdulus Operator it requires two arguments and both arguments must be Integer type.
  • We cannot apply Modulus operator on Float Datatype. If we used it on Float Datatype we will get Compilation Error.
Here is the Example on this issue.
Example :
  1. int main()
  2. {
  3.         int a; float b;
  4.         printf(“Result is %d n, a % b );
  5.         return 0;
  6. }
Output :

error: invalid operands to binary % (have ‘int’ and ‘float’)

How find Modulus of Floating Type Data: 

  • If we need to find remainder of Float  type data we can take the help of Library functions fmod( ) and fmodl().
  • fmod() and fmodl are declared in math.h.
  • By using fmod() we can find Remainder of float type and Double type data.
  • By using fmodl() we can find Remainder of long Double type data.
Here is the Example based on fmod..

Prototype or Syntax of fmod and fmodl :

double fmod(double x, double y);
long double fmodl(long double x, long double y);

 Example :

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main(void) {
  4.         float f = 5.5;
  5.         int a = 3;
  6.         printf(“Result is %f n,fmod(f,a));
  7.         return 0;
  8. }

Output :
Result is 2.500000

That’s it about Modulus operator in C.. If you have any doubt feel free to leave a Comment..
Are you understand Every thing Let’s take a quick online test ===>Test your Arithmetic Operators Skills .

See Also :

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

1 Response

Leave a Reply