📖 Scenario: Imagine you are working with a clock that shows numbers from 0 to 11. When the hour passes 11, it goes back to 0. This is an example of modular arithmetic.We will learn how to use modular arithmetic to find the remainder when numbers wrap around a fixed size, like hours on a clock.
🎯 Goal: You will create a small program that calculates the remainder of numbers when divided by a fixed number (called the modulus). This helps understand how numbers wrap around in modular arithmetic.
📋 What You'll Learn
Create a variable with a number to divide
Create a variable for the modulus (the number to divide by)
Calculate the remainder using modular arithmetic
Print the result showing the remainder
💡 Why This Matters
🌍 Real World
Modular arithmetic is used in clocks, calendars, computer hashing, and cryptography to handle numbers that wrap around fixed limits.
💼 Career
Understanding modular arithmetic is important for software developers working with algorithms, security, and systems programming.
Progress0 / 4 steps
1
Create the number to divide
Create a variable called number and set it to 29.
DSA Typescript
Hint
Use const number = 29; to create the variable.
2
Create the modulus value
Create a variable called modulus and set it to 12.
DSA Typescript
Hint
Use const modulus = 12; to create the modulus variable.
3
Calculate the remainder using modular arithmetic
Create a variable called remainder and set it to the remainder of number divided by modulus using the % operator.
DSA Typescript
Hint
Use const remainder = number % modulus; to calculate the remainder.
4
Print the remainder result
Use console.log to print the text "Remainder: " followed by the value of remainder.
DSA Typescript
Hint
Use console.log("Remainder: " + remainder); to print the result.
Practice
(1/5)
1. What is the result of 7 % 3 in modular arithmetic?
easy
A. 2
B. 1
C. 3
D. 0
Solution
Step 1: Understand the modulo operation
The modulo operator (%) gives the remainder when the first number is divided by the second.
Step 2: Calculate 7 % 3
7 divided by 3 is 2 with a remainder of 1, but since 7 = 3*2 + 1, the remainder is 1.
Final Answer:
1 -> Option A
Quick Check:
7 % 3 = 1 [OK]
Hint: Modulo gives remainder after division [OK]
Common Mistakes:
Confusing remainder with quotient
Thinking modulo returns division result
Mixing up dividend and divisor
2. Which of the following is the correct TypeScript syntax to compute the remainder of a divided by b?
easy
A. let result = a % b;
B. let result = a / b;
C. let result = a mod b;
D. let result = a & b;
Solution
Step 1: Recall TypeScript modulo operator
In TypeScript, the modulo operator is represented by the percent sign (%).
Step 2: Identify correct syntax
let result = a % b; uses a % b, which correctly computes the remainder.
Final Answer:
let result = a % b; -> Option A
Quick Check:
Modulo operator is % in TypeScript [OK]
Hint: Use % for modulo in TypeScript [OK]
Common Mistakes:
Using / instead of %
Using 'mod' keyword which doesn't exist
Using bitwise & operator by mistake
3. What is the output of the following TypeScript code?
const a = 14;
const b = 5;
console.log(a % b);
medium
A. 3
B. 4
C. 2
D. 0
Solution
Step 1: Calculate 14 % 5
Divide 14 by 5: 5 * 2 = 10, remainder is 14 - 10 = 4.
Step 2: Confirm output
The console.log prints the remainder, which is 4.
Final Answer:
4 -> Option B
Quick Check:
14 % 5 = 4 [OK]
Hint: Modulo is remainder after division [OK]
Common Mistakes:
Calculating quotient instead of remainder
Mixing up operands order
Assuming modulo returns division result
4. Identify the error in this TypeScript code snippet that tries to compute modulo:
function mod(a: number, b: number): number {
return a % b;
}
console.log(mod(10, 0));
medium
A. Syntax error due to missing semicolon
B. No error, outputs 0
C. Runtime error: division by zero
D. Returns NaN without error
Solution
Step 1: Analyze modulo with zero divisor
In TypeScript/JavaScript, modulo by zero returns NaN and does NOT throw a runtime error.
Step 2: Understand error behavior
Calling mod(10, 0) computes 10 % 0 which is NaN, and console.log prints NaN.
Final Answer:
Returns NaN without error -> Option D
Quick Check:
10 % 0 = NaN [OK]
Hint: Modulo by zero returns NaN in TypeScript [OK]
Common Mistakes:
Thinking modulo by zero causes runtime error
Assuming modulo by zero returns 0
Confusing with languages that raise exceptions
5. You want to find the remainder when the sum of two large numbers a = 123456789 and b = 987654321 is divided by m = 1000. Which expression correctly uses modular arithmetic to compute (a + b) % m without overflow?
hard
A. (a + b) % m
B. a % (b % m)
C. (a % m + b % m) % m
D. (a % b) + (b % m)
Solution
Step 1: Understand modular addition property
Modular arithmetic allows splitting sums: (a + b) % m = ((a % m) + (b % m)) % m.
Step 2: Apply property to avoid overflow
Computing (a % m + b % m) % m prevents large intermediate sums and overflow.
Step 3: Check other options
(a + b) % m risks overflow; B and D misuse modulo operations.
Final Answer:
(a % m + b % m) % m -> Option C
Quick Check:
Use modular addition property [OK]
Hint: Add remainders then modulo again to avoid overflow [OK]
Common Mistakes:
Directly adding large numbers before modulo
Misusing modulo on operands incorrectly
Ignoring overflow in large sums
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.