Modulus Operator in C Programming with Example programs

Spread the love
Modulus-Operator-in-C-Language-with-examples

Introduction:

In our previous article, We have learned about the Division Operator in C Programming. In today’s article, We will continue with arithmetic operators and discuss the Modulus Operator in C Language.

What is Modulus Operator:

Modulus Operator gives the Remainder of the division of one number by another. 

C Programming Language supports the modulus operator. Which is used to calculate the remainder of two values.

We denote the modulus operator using the Percentage ( % ) Symbol in C programming.

Modulus Operator Examples:

Let’s say we have two numbers 11 and 5,

The Modulus of 11 and 5 is 11 % 5 which is equal to 1.

As we discussed earlier, the Modulus operator returns the remainder value ( after division) as the result.

Here, The statement 11 % 5 will give us the result as 1, Because 11 divided by 5 gives us the quotient as 2 and remainder as 1.

11 % 5 = 1

Modulus Operator example Program:

Let’s write a simple program to demonstrate the modulus operator.

Program Output:

The output of the above program is 1. The remainder of the 11 % 5 Operation.

Modulus Operator results Sign:

Modulus Operator output sign always depends upon the Numerator sign only.

So the resulting remainder sign will be based on the Numerator sign.

Program – Modulus operator Sign:

Program Output:

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

Modulus Operator when Numerator less than Denominator:

When the Numerator Value is less than the denominator Value, then the modulus operator result will be Numerator Value.

For example,

44 % 100

Here, The 44 is less than the 100, So the modulus operator result will be 44.

Example Program :

Output:

No Modulus Operator for Float Data:

The C Language modulus operator is not allowed on floating-point data.

Modulus Operators both arguments i.e numerator and denominator must be integer values.

Using floating-point values with The modulus operator will result in the Compilation error.

Here is an example program,

Program with Float data:

In this program, We are going to calculate the modulus of the integer variable and float variable.

Program Output:

As the modulus operator won’t accept the float values, the above program will generate a compilation error.

How to calculate the Modulus of Float Data in C:

If you want to calculate the modulus of the floating-point data in C language, Then we need to use the fmod() and fmodl() library functions.

The fmod() and fmodl() functions are available as part of the math.h library.

fmod function:

Syntax of fmod function.

The fmod() function can be used to calculate the Modulus or Remainder of the float datatype and Double datatype values.

fmodl function:

Syntax of fmodl function

Similarly, The fmodl() function can calculate the Modulus of Long Double data values.

Program to calculate the modulus of Float data:

In this program, We are calculating the modulus of the float data using math.h fmod function.

Output:

As you can from the output, We got the desired result of 2.500000

So by using the fmod and fmodl functions we can calculate the modulus of the floating-point data.

Related Articles:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. November 29, 2018

    […] Modulus Operator […]

  2. June 20, 2021

    […] Modulus Operator […]

  3. July 28, 2021

    […] Modulus Operator in C Programming with Example programs – SillyCodes […]

Leave a Reply