Bird
Raised Fist0
DSA Cprogramming~10 mins

Modular Arithmetic Basics in DSA C - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compute the remainder of 10 divided by 3.

DSA C
int result = 10 [1] 3;
printf("%d\n", result);
Drag options to blanks, or click blank then click option'
A+
B*
C/
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using / instead of % will give the quotient, not remainder.
Using * or + will not compute remainder.
2fill in blank
medium

Complete the code to compute (7 + 5) modulo 6.

DSA C
int sum = (7 + 5) [1] 6;
printf("%d\n", sum);
Drag options to blanks, or click blank then click option'
A%
B*
C/
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using / will give quotient, not remainder.
Using * or - will not compute modulo.
3fill in blank
hard

Fix the error in the code to correctly compute (a * b) modulo m.

DSA C
int mod_mul(int a, int b, int m) {
    return (a [1] b) % m;
}
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - will add or subtract instead of multiply.
Using / will cause division error or wrong result.
4fill in blank
hard

Fill both blanks to compute (x - y) modulo n correctly, handling negative results.

DSA C
int mod_sub(int x, int y, int n) {
    int diff = (x [1] y) % n;
    return diff [2] 0 ? diff + n : diff;
}
Drag options to blanks, or click blank then click option'
A-
B<
C>
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - for subtraction.
Checking > 0 instead of < 0 for negative.
5fill in blank
hard

Fill all three blanks to create a function that computes (a^b) modulo m using fast exponentiation.

DSA C
int mod_pow(int a, int b, int m) {
    int result = 1;
    a = a [1] m;
    while (b > 0) {
        if (b [2] 1) result = (result * a) % m;
        a = (a * a) % m;
        b = b [3] 1;
    }
    return result;
}
Drag options to blanks, or click blank then click option'
A%
B&
C>>
D<<
Attempts:
3 left
💡 Hint
Common Mistakes
Using / instead of >> for shifting.
Using + or - instead of & for bit check.
Not applying modulo after multiplication.

Practice

(1/5)
1. What is the result of 7 % 3 in modular arithmetic?
easy
A. 1
B. 2
C. 3
D. 0

Solution

  1. Step 1: Understand the modulo operation

    The modulo operator (%) gives the remainder when the first number is divided by the second.
  2. Step 2: Calculate 7 % 3

    7 divided by 3 is 2 with a remainder of 1, but since 7 % 3 means remainder, it is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    7 % 3 = 1 [OK]
Hint: Modulo gives remainder after division [OK]
Common Mistakes:
  • Confusing quotient with remainder
  • Using division instead of modulo
  • Mixing up dividend and divisor
2. Which of the following is the correct syntax to compute the remainder of a divided by b in C?
easy
A. a mod b
B. a % b
C. mod(a, b)
D. a / b

Solution

  1. Step 1: Recall C syntax for modulo

    In C, the modulo operator is represented by the percent sign '%'.
  2. Step 2: Identify correct syntax

    The correct way to get remainder is a % b. Other options are invalid syntax or division.
  3. Final Answer:

    a % b -> Option B
  4. Quick Check:

    Modulo operator in C is % [OK]
Hint: Use % operator for modulo in C [OK]
Common Mistakes:
  • Using 'mod' keyword
  • Using division operator '/' instead
  • Calling a function mod() which doesn't exist in C
3. What is the output of the following C code?
#include 
int main() {
    int x = 14;
    int y = 5;
    printf("%d", (x + y) % y);
    return 0;
}
medium
A. 4
B. 0
C. 1
D. 5

Solution

  1. Step 1: Calculate (x + y)

    x = 14, y = 5, so x + y = 19.
  2. Step 2: Calculate (x + y) % y

    19 % 5 is the remainder when 19 is divided by 5. 5 * 3 = 15, remainder 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    (14 + 5) % 5 = 4 [OK]
Hint: Add first, then apply modulo [OK]
Common Mistakes:
  • Applying modulo before addition
  • Confusing remainder with quotient
  • Incorrect operator precedence
4. Identify the error in the following C code snippet that attempts to compute a % b:
int a = 10;
int b = 0;
int c = a % b;
printf("%d", c);
medium
A. Syntax error in modulo operator
B. Variable c is not declared
C. Division by zero error
D. No error, code runs fine

Solution

  1. Step 1: Check divisor value

    Variable b is zero, which is the divisor in modulo operation.
  2. Step 2: Understand modulo with zero divisor

    Modulo by zero is undefined and causes a runtime error (division by zero).
  3. Final Answer:

    Division by zero error -> Option C
  4. Quick Check:

    Modulo divisor cannot be zero [OK]
Hint: Never modulo by zero; it causes runtime error [OK]
Common Mistakes:
  • Ignoring zero divisor
  • Assuming modulo by zero returns zero
  • Confusing syntax error with runtime error
5. Given the expression (a * b) % m, which of the following is a correct way to compute it without causing integer overflow in C when a and b are large integers?
hard
A. Use (a % b) % m
B. Use (a * b) % m directly
C. Use (a + b) % m
D. Use ((a % m) * (b % m)) % m

Solution

  1. Step 1: Understand overflow risk

    Multiplying large integers a * b can overflow before modulo is applied.
  2. Step 2: Apply modular multiplication property

    Using ((a % m) * (b % m)) % m keeps intermediate values smaller, preventing overflow.
  3. Final Answer:

    ((a % m) * (b % m)) % m -> Option D
  4. Quick Check:

    Modular multiplication reduces overflow risk [OK]
Hint: Modulo each operand before multiply to avoid overflow [OK]
Common Mistakes:
  • Multiplying before modulo causing overflow
  • Using addition instead of multiplication
  • Incorrect modulo order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard