Beginner C/C++ Exercises

Convert Decimal to Binary in C and C++

Divide by 2, keep the remainders, read them backwards: 13 becomes 1101.

  • Beginner
  • 2 min read
  • 2 runnable examples
What you'll learn
  • Repeated division by 2
  • Collecting remainders
  • Printing the digits in reverse
13 → binary
  1. 13 / 2 = 6remainder 1lowest bit
  2. 6 / 2 = 3remainder 0
  3. 3 / 2 = 1remainder 1
  4. 1 / 2 = 0remainder 1highest bit
Read the remainders from bottom to top: 1101.

Remainders give the bits

The problem

Computers store everything in binary, and understanding how a number like 13 becomes 1101 helps with bit masks, file formats and debugging. The conversion produces digits in reverse order, which needs handling.

Think of it like this

Converting to binary is like splitting a pile of coins into pairs again and again. Each time, you note whether one coin is left over (1) or not (0). The notes come out from the last digit to the first, so you read them backwards.

From pairing coins to code
value % 2🪙 Is one coin left over?
value /= 2👫 Keep only the pairs
bits[count++]📝 Write down the leftover
bits[--count]🔙 Read the notes backwards: 1101
Collect remainders of division by 2, then print them in reverse.
In one sentence

Converting a decimal number to binary repeatedly divides it by 2, records each remainder, and reads the remainders in reverse order.

Important points

  1. value % 2 gives the lowest binary digit, and value /= 2 removes it.
  2. bits[count++] stores the digits from right to left.
  3. The do-while loop runs at least once, so 0 prints as "0".
  4. bits[--count] prints the stored digits in reverse, giving 1101.

When to use it

  • Understanding binary numbers and bit operations.
  • Printing numbers in other bases, such as 8 or 16.
  • Practicing arrays used as a stack.

When not to use it

  • C++ code: std::bitset<8>(13) prints binary directly.
  • Negative numbers: they need two's complement handling.

C and C++ code examples

C
Run code →
main.c
#include <stdio.h>
int main(void) {
    unsigned number = 13, value = number; int bits[32], count = 0;
    // Store remainders generated from right to left.
    do { bits[count++] = value % 2; value /= 2; } while (value);
    printf("%u = ", number); while (count) printf("%d", bits[--count]); puts("");
}
Get a Run button for this example Result of running the code
13 = 1101
C++
Run code →
main.cpp
#include <bitset>
#include <iostream>
#include <string>
int main() {
    unsigned number = 13;
    // Remove leading zeroes from a fixed-width bitset string.
    std::string bits = std::bitset<32>(number).to_string();
    bits.erase(0, bits.find_first_not_of('0'));
    std::cout << number << " = " << bits << '\n';
}
Get a Run button for this example Result of running the code
13 = 1101

C and C++ comparison

Dividing 13 by 2 again and again gives the remainders 1, 0, 1, 1, which are the binary digits from right to left. The do-while loop stores them in bits. Printing the array backwards shows 1101.

C

C exposes small procedural APIs and makes representation details explicit.

C++

C++ retains the low-level model while adding safer library types and abstractions.

Practice exercises

Run both versions, then modify them to observe the different language guarantees.

  • Add invalid and boundary-value inputs.
  • Compile with -Wall -Wextra -Wpedantic.
  • Move reusable declarations into a header and implementation file.

Common mistakes

Printing digits in the order found

The first remainder is the last digit, so 13 would print as 1011.

Problem
while (value) { printf("%u", value % 2); value /= 2; }
Fix
do { bits[count++] = value % 2; value /= 2; } while (value);
while (count) printf("%d", bits[--count]);

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges