Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Differentiate the modulo and division by using C Programming language?
In C programming, the modulo and division operators are fundamental arithmetic operators that work with integers but produce different results from the same operands.
Modulo (%) − Returns the remainder after integer division.
Division (/) − Returns the quotient (whole number result) of integer division.
Syntax
result = dividend % divisor; // Modulo operation result = dividend / divisor; // Division operation
Example 1: Basic Modulo and Division
This example demonstrates the difference between modulo and division operators −
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("a/b = %d (quotient)<br>", a/b);
printf("a%%b = %d (remainder)<br>", a%b);
printf("(a+10)/b = %d<br>", (a+10)/b);
printf("(a+10)%%b = %d<br>", (a+10)%b);
return 0;
}
Enter two numbers: 17 5 a/b = 3 (quotient) a%b = 2 (remainder) (a+10)/b = 5 (a+10)%b = 2
Example 2: Using Pointers for Modulo and Division
This example shows how to perform modulo and division operations using pointer variables −
#include <stdio.h>
int main() {
int num1, num2;
int *p1, *p2;
int div, mod;
/* Assigning addresses to pointers */
p1 = &num1;
p2 = &num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
/* Performing operations using pointers */
div = *p1 / *p2;
mod = *p1 % *p2;
printf("Division result = %d<br>", div);
printf("Modulo result = %d<br>", mod);
return 0;
}
Enter two numbers: 30 7 Division result = 4 Modulo result = 2
Key Differences
| Operator | Symbol | Purpose | Example (17 ÷ 5) | Result |
|---|---|---|---|---|
| Division | / | Returns quotient | 17 / 5 | 3 |
| Modulo | % | Returns remainder | 17 % 5 | 2 |
Conclusion
The division operator (/) gives the quotient of integer division, while the modulo operator (%) gives the remainder. Both operators are essential for mathematical computations and algorithmic problem-solving in C programming.
Advertisements
