×

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

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 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.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.