Bird
0
0
DSA Cprogramming~10 mins

Modular Arithmetic Basics in DSA C - Interactive Practice

Choose your learning style9 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.