Modulo Operator (%) in C/C++ with Examples - GeeksforGeeks (2024)

Last Updated : 30 Oct, 2023

Improve

In C or C++, the modulo operator (also known as the modulus operator), denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division which is also called the modulus of the operation.

Syntax of Modulus Operator

If x and y are integers, then the expression:

x % y;

pronounced as “x mod y”. For example, 10 % 2 will be pronounced as ” Ten mod Two”.

Return Value of Modulo Operator

  • If y completely divides x, the result of the expression is 0.
  • If x is not completely divisible by y, then the result will be the remainder in the range [0, y-1]
  • (x % y) < (x / 2) ………if (x >= y)
  • (x % y) = x ……… if (x < y)
  • If y is 0, then division by zero is a compile-time error.

Example of Modulo Operator

Below is the C/C++ program to demonstrate the working of the modulo operator:

C++

// C++ Program to demonstrate the working of modulo operator

#include <iostream>

using namespace std;

// Driver code

int main(void)

{

int x, y;

int result;

x = 3;

y = 4;

// using modulo operator

result = x % y;

cout << result << endl;

result = y % x;

cout << result << endl;

// for different values

x = 4;

y = 2;

result = x % y;

cout << result;

return 0;

}

// This code is contributed by Mayank Tyagi

C

// C Program to illustrate the working of modulo operator

#include <stdio.h>

int main(void)

{

int x, y;

int result;

x = 3;

y = 4;

// using modulo operator

result = x % y;

printf("%d", result);

result = y % x;

printf("\n%d", result);

// for different values

x = 4;

y = 2;

result = x % y;

printf("\n%d", result);

return 0;

}

Restrictions on the Modulo Operator

The modulo operator has few restrictions or limitations on it. The % modulus operator cannot be applied to floating-point numbers i.e. float or double. If you try to use the modulo operator with floating-point constants or variables, the compiler will produce an error.

Example 1: C/C++ program to demonstrate the restrictions of the modulo operator.

C++

// C++ Program to demonstrate the restrictions of modulo

// operator

#include <iostream>

using namespace std;

// Driver code

int main()

{

float x, y;

x = 2.3;

y = 1.5;

// modulo for floating point values

result = x % y;

cout << result;

return 0;

}

// This code is contributed by Harsh*t Srivastava

C

// C Program to illustrate the working of modulo operator

#include <stdio.h>

int main(void)

{

float x, y;

float result;

x = 2.3;

y = 1.5;

// modulo for floating point values

result = x % y;

printf("%f", result);

return 0;

}

Output

Compilation Error in C code :- prog.c: In function 'main':
prog.c:19:16: error:
invalid operands to binary % (have 'float' and 'float')
result = x % y;
^

Modulo Operator for Negative Operands

The sign of the result for the modulo operator is machine-dependent for negative operands, as the action takes as a result of underflow or overflow.

Example 2: C/C++ program to demonstrate the modulo operator for negative operands.

C++

// C++ Program to demonstrate the working of the modulo

// operator for negative operands

#include <iostream>

using namespace std;

// Driver code

int main(void)

{

int x, y;

int result;

x = -3;

y = 4;

// modulo for negative operands

result = x % y;

cout << result << endl;

x = 4;

y = -2;

result = x % y;

cout << result << endl;

x = -3;

y = -4;

result = x % y;

cout << result;

return 0;

}

// This code is contributed by Harsh*t Srivastava

C

// C Program to illustrate the working of the modulo

// operator with negative operands

#include <stdio.h>

int main(void)

{

int x, y;

int result;

x = -3;

y = 4;

// modulo for negative operands

result = x % y;

printf("%d", result);

x = 4;

y = -2;

result = x % y;

printf("\n%d", result);

x = -3;

y = -4;

result = x % y;

printf("\n%d", result);

return 0;

}

Output

-30-3

Note: The return value in this case is compiler dependent.

FAQs on Modulo Operator

Q1. Define mod.

Answer:

In C/C++ programming languages, mod refers to the mathematical operation in which one number is divided by another, and the remainder is returned.

It can be performed using modulo operator (%).

Q2. What is mod arithmetic?

Answer:

Mod arithmetic refers to the process in which a number keeps wrapping around a certain point in such a way that it is always less than that certain point. For example,

Consider the number n = 10 and the point p = 20.
When we increment n 10 times, it will be n = 20 but in modular arithmetic, it should ways be smaller that the specified point.

One way to do that is to use modulo operator as:

n++;
n = n % p;

To learn more about modular aritimatic, refer to the article – Modular Arithmatic

Q3. What is the difference between modulo and divide operator?

Answer:

The major difference between modulo and division operator is that:

  • Modulo Operator (%) returns the remainder after dividing one number with other.
  • Divide Operator (/) returns the quotient after dividing one number with other.


Like Article

Suggest improvement

Next

Manipulators in C++ with Examples

Share your thoughts in the comments

Please Login to comment...

Modulo Operator (%) in C/C++ with Examples - GeeksforGeeks (2024)
Top Articles
Hartford, CT - Non-Fatal Pedestrian Collision at Garden St and Mather St
Journal articles: 'Boston College High School (Mass.)' – Grafiati
Mickey Moniak Walk Up Song
Devin Mansen Obituary
Canya 7 Drawer Dresser
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Jazmen Jafar Linkedin
What to Serve with Lasagna (80+ side dishes and wine pairings)
The Potter Enterprise from Coudersport, Pennsylvania
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Optimal Perks Rs3
Hallowed Sepulchre Instances &amp; More
Oppenheimer & Co. Inc. Buys Shares of 798,472 AST SpaceMobile, Inc. (NASDAQ:ASTS)
Which aspects are important in sales |#1 Prospection
Revitalising marine ecosystems: D-Shape’s innovative 3D-printed reef restoration solution - StartmeupHK
Myql Loan Login
Cvs Learnet Modules
Pro Groom Prices – The Pet Centre
What is the difference between a T-bill and a T note?
Conan Exiles Thrall Master Build: Best Attributes, Armor, Skills, More
Connect U Of M Dearborn
Haunted Mansion Showtimes Near Millstone 14
Pricelinerewardsvisa Com Activate
Where to Find Scavs in Customs in Escape from Tarkov
Morristown Daily Record Obituary
Dragonvale Valor Dragon
Rapv Springfield Ma
Best Boston Pizza Places
Avatar: The Way Of Water Showtimes Near Maya Pittsburg Cinemas
Arrest Gif
Impact-Messung für bessere Ergebnisse « impact investing magazin
Mdt Bus Tracker 27
Table To Formula Calculator
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Pdx Weather Noaa
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Kaiserhrconnect
Rock Salt Font Free by Sideshow » Font Squirrel
Fridley Tsa Precheck
Chattanooga Booking Report
All Things Algebra Unit 3 Homework 2 Answer Key
Keeper Of The Lost Cities Series - Shannon Messenger
Restored Republic May 14 2023
Wrigley Rooftops Promo Code
303-615-0055
Man Stuff Idaho
Nid Lcms
Clausen's Car Wash
Mudfin Village Wow
Guy Ritchie's The Covenant Showtimes Near Grand Theatres - Bismarck
Rovert Wrestling
60 Second Burger Run Unblocked
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6069

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.