Base 7 - Problem

Given an integer num, return a string of its base 7 representation.

Base 7 uses digits 0-6, where each position represents a power of 7. For example, the number 100 in base 10 equals 2*7² + 0*7¹ + 2*7⁰ = 202 in base 7.

Input & Output

Example 1 — Positive Number
$ Input: num = 100
Output: "202"
💡 Note: 100 ÷ 7 = 14 remainder 2, 14 ÷ 7 = 2 remainder 0, 2 ÷ 7 = 0 remainder 2. Reading remainders from bottom to top: "202"
Example 2 — Negative Number
$ Input: num = -7
Output: "-10"
💡 Note: Convert absolute value: 7 ÷ 7 = 1 remainder 0, 1 ÷ 7 = 0 remainder 1. Result is "10", add minus sign: "-10"
Example 3 — Zero
$ Input: num = 0
Output: "0"
💡 Note: Special case: 0 in any base is "0"

Constraints

  • -107 ≤ num ≤ 107

Visualization

Tap to expand
Base 7 Conversion ProcessInput100(Decimal)Division Process100 ÷ 7 = 14 R 214 ÷ 7 = 2 R 02 ÷ 7 = 0 R 2Output"202"(Base 7)Remainders (bottom to top): 2, 0, 2Verification: 2×7² + 0×7¹ + 2×7⁰ = 98 + 0 + 2 = 100 ✓
Understanding the Visualization
1
Input
Decimal number (e.g., 100)
2
Process
Repeated division by 7, collecting remainders
3
Output
Base 7 string ("202")
Key Takeaway
🎯 Key Insight: Base conversion uses repeated division - remainders become digits in reverse order
Asked in
Google 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen