C Remainder Assignment Operator
In C, the Remainder Assignment %=
operator is a compound assignment operator used to update the value of a variable by computing the remainder when divided by another value.
Remainder Assignment is a shorthand for dividing the variable by another number and assigning the remainder back to the variable.
Syntax of the Remainder Assignment Operator
The syntax to use the Remainder Assignment operator is:
variable %= value;
Explanation:
variable
: The left-hand operand whose value will be updated.%=
: The remainder assignment operator.value
: The right-hand operand by whichvariable
will be divided.
Examples of the Remainder Assignment Operator
1. Basic Usage of the Remainder Assignment Operator
In this example, we will use the %=
operator to find the remainder of a number when divided by another number.
main.c
#include <stdio.h>
int main() {
int num = 10;
num %= 3; // num = num % 3
printf("Updated value of num: %d\n", num);
return 0;
}
Explanation:
- We declare an integer variable
num
and initialize it with 10. - The remainder assignment operation
num %= 3
calculates10 % 3
, which gives 1. - The result (1) is assigned back to
num
. - We print the updated value of
num
, which is now 1.
Output:
Updated value of num: 1
2. Using Remainder Assignment in a Loop
In this example, we will use the %=
operator inside a loop to process numbers.
main.c
#include <stdio.h>
int main() {
int numbers[] = {15, 28, 36, 49, 55};
int size = sizeof(numbers) / sizeof(numbers[0]);
for (int i = 0; i < size; i++) {
numbers[i] %= 5; // Find remainder when divided by 5
}
// Printing modified array
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Explanation:
- We declare an integer array
numbers[]
with five elements. - We determine the size of the array using
sizeof(numbers) / sizeof(numbers[0])
. - A
for
loop iterates through the array. - Inside the loop, each element is updated using
numbers[i] %= 5
, which stores the remainder when divided by 5. - After the loop, we print the modified array.
Output:
0 3 1 4 0
3. Using Remainder Assignment in Conditional Statements
In this example, we will use the %=
operator with an if
condition to check divisibility.
main.c
#include <stdio.h>
int main() {
int num = 24;
if ((num %= 2) == 0) {
printf("Number is even.\n");
} else {
printf("Number is odd.\n");
}
return 0;
}
Explanation:
- We declare an integer variable
num
and initialize it with 24. - The expression
(num %= 2)
calculates24 % 2
, which results in 0. - The result is compared to 0 inside the
if
condition. - Since the result is 0, “Number is even.” is printed.
Output:
Number is even.
Conclusion
In this tutorial, we covered the Remainder Assignment %=
operator in C:
- The
%=
operator computes the remainder when dividing a variable by a number and stores the result in the variable. - It is useful for finding remainders in arithmetic operations, loops, and conditional statements.
- We explored three use cases: basic usage, modifying an array in a loop, and using it in an
if
condition.