×

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number.

What is Modulus of Negative number?

By omitting the minus sign, one can determine the modulus of a negative number. Adding vertical lines all around a number indicates the modulus of that number. Also keep in mind that multiplying a negative number by one will produce its modulus, like in the case of the expression (-9) = 9.

Modulus means:

The remainder of a division is represented by the modulo, or simply "mod." Gauss wrote a treatise on modular arithmetic that was released in 1801. Later, Donald Knuth provided a mathematical definition that is now universally recognised.

The result of a division is returned by the modulo operator. But when negative numbers are added, things become a little bit more complicated.

Let’s look at some examples on modulus of negative numbers. And see the output of the programs.

Example 1:

C Program

#include <stdio.h>


// Main function
int main ()
{
// Declaring and initializing the variables
int x = 7, y = - 11, z = 2;


// Calculating and printing
printf ( "%d", x % y / z );
}

C++ Program

#include <iostream>
using namespace std;


// Main function
int main ()
{
// Declaring and initializing the variables
int x = 7, y = - 11, z = 2;


// Calculating and printing
cout << x % y / z ;
}

Output:

3

The precedence for % and / is the same. % and / present left-to-right associativity. Therefore, percent is carried out initially. The significance is that, in the case of the modulus operator, a sign of a left operand is attached to the result.

Example 2:

C Program

#include <stdio.h>


// Main function
int main ()
{
 // Declaring and initializing the variables
 // x is positive and y is negative.
   int x = 4, y = -9 ;


// Calculating and printing
printf ("%d" , x % y ) ;
}

C++ Program

#include <iostream>
using namespace std;


// Main function
int main ()
{
 // Declaring and initializing the variables
 // x is positive and y is negative.
   int x = 4, y = -9 ;


// Calculating and printing
cout << x % y ;
}

Output:

4

Example 3:

C Program

#include <stdio.h>


// Main function
int main ()
{
 // Declaring and initializing the variables
 // x is negative and y is positive.
   int x = -4, y = 9 ;


// Calculating and printing
printf ("%d" , x % y ) ;
}

C++ Program

#include <iostream>
using namespace std;


// Main function
int main ()
{
 // Declaring and initializing the variables
 // x is negative and y is positive.
   int x = -4, y = 9 ;


// Calculating and printing
cout << x % y ;
}

Output:

-4

However, according to the meaning of the term, the remainder is always an integer that should be subtracted from x to make it divisible by y.

Therefore, since -4 is negative, it is not the genuine remainder in the case above.

To avoid negative remainder, we must always find the remainder in the C/C++ language as (x % y + y) % y (add quotient to remainder and again take remainder).

Example 4:

C Program

#include <stdio.h>


// Main function
int main ()
{
 // Declaring and initializing the variables
 // x is negative and y is negative.
   int x = -4, y = -9 ;


// Calculating and printing
printf ("%d" , x % y ) ;
}

C++ Program

#include <iostream>
using namespace std;


// Main function
int main ()
{
 // Declaring and initializing the variables
 // x is negative and y is negative.
   int x = -4, y = -9 ;


// Calculating and printing
cout << x % y ;
}

Output:

-4

When both operands are positive, all can guess what a modulus operator would generate. But different languages provide different results when it comes to negative numbers.

The modulus in C is calculated as,

x % n = x – ( n * trunc( x / n ) ).


For example,
9 % -4 = 9 – ( -4 * trunc ( 9 / -4 ) )
           = 9 – ( -4 * trunc ( -2.25 ) )
           = 9 – ( -4 * -2 ) { rounded off }
           = 9 – 6
           = 3

Chart:

NumeratorDenominator
XYX / YX % Y
++++
+--+
-+--
--+-

We can learn from the above chart that the % operator always takes a numerator's sign into account.


Related Topics

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.