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:

</>
Copy
variable %= value;

Explanation:

  1. variable: The left-hand operand whose value will be updated.
  2. %=: The remainder assignment operator.
  3. value: The right-hand operand by which variable 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

</>
Copy
#include <stdio.h>

int main() {
    int num = 10;
    num %= 3;  // num = num % 3

    printf("Updated value of num: %d\n", num);

    return 0;
}

Explanation:

  1. We declare an integer variable num and initialize it with 10.
  2. The remainder assignment operation num %= 3 calculates 10 % 3, which gives 1.
  3. The result (1) is assigned back to num.
  4. 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

</>
Copy
#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:

  1. We declare an integer array numbers[] with five elements.
  2. We determine the size of the array using sizeof(numbers) / sizeof(numbers[0]).
  3. A for loop iterates through the array.
  4. Inside the loop, each element is updated using numbers[i] %= 5, which stores the remainder when divided by 5.
  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

</>
Copy
#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:

  1. We declare an integer variable num and initialize it with 24.
  2. The expression (num %= 2) calculates 24 % 2, which results in 0.
  3. The result is compared to 0 inside the if condition.
  4. 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:

  1. The %= operator computes the remainder when dividing a variable by a number and stores the result in the variable.
  2. It is useful for finding remainders in arithmetic operations, loops, and conditional statements.
  3. We explored three use cases: basic usage, modifying an array in a loop, and using it in an if condition.