📘 Lesson  ·  Lesson 66

Decimal to Binary

The idea behind the conversion

Our everyday numbers are in base 10 (decimal), using digits 0-9. Computers use base 2 (binary), using only 0 and 1. Converting a decimal number to binary means rewriting it using only those two digits.

The trick is simple: keep dividing the number by 2 and note the remainder each time. Each remainder is a binary digit (a bit). When the number reaches 0, you have all the bits — you just read them in reverse. Let's see exactly why with a dry run.

Step-by-step dry run

Let's convert 13 to binary by hand, dividing by 2 each step:

StepNumber÷ 2 quotientRemainder (bit)
11361
2630
3311
4101

The remainders in the order we found them are 1, 0, 1, 1. Reading them bottom to top (reverse) gives 1101 — that is 13 in binary.

Method 1: divide by 2 (array)

The most common program stores each remainder in an array, then prints the array backwards.

C Language
#include <stdio.h>
int main() {
    int n = 13, binary[32], i = 0;

    if (n == 0) { printf("0"); return 0; }  // handle zero

    while (n > 0) {
        binary[i] = n % 2;   // save the remainder
        n = n / 2;           // move to next
        i++;
    }
    // print in reverse
    for (int j = i - 1; j >= 0; j--)
        printf("%d", binary[j]);
    return 0;
}
Output:
1101

The loop fills the array with 1, 0, 1, 1; the reverse-print turns it into 1101.

Method 2: using recursion

Recursion removes the array entirely. By calling itself before printing, the bits naturally come out in the right order.

C Language
#include <stdio.h>
void toBinary(int n) {
    if (n == 0) return;
    toBinary(n / 2);      // go deeper first
    printf("%d", n % 2);  // print on the way back
}
int main() {
    int n = 13;
    if (n == 0) printf("0");
    else toBinary(n);
    return 0;
}
Output:
1101
💡 Why it works

Because the function calls itself before printing, the deepest call (the most significant bit) prints first. The reversal happens for free.

Method 3: using bitwise operators

You can also inspect each bit directly with the right-shift >> and AND & operators.

C Language
#include <stdio.h>
int main() {
    int n = 13;
    int started = 0;
    for (int i = 31; i >= 0; i--) {
        int bit = (n >> i) & 1;   // read the i-th bit
        if (bit) started = 1;
        if (started) printf("%d", bit);
    }
    return 0;
}
Output:
1101

This walks from the highest bit down, skipping leading zeros until the first 1 appears. It is closest to how the computer actually stores the number.

Quick reference: decimal to binary

DecimalBinary
210
5101
81000
101010
131101
1610000

Common mistakes

  • Printing the remainders in the order collected instead of reversing them.
  • Forgetting the special case for 0, which prints nothing otherwise.
  • Using too small an array for large numbers (32 bits is a safe size for an int).
  • Confusing n % 2 (the bit) with n / 2 (the next number).
🏋️ Practice

Modify Method 1 to read the number from the user with scanf, and test it with 0, 1, 25 and 255. Confirm 255 gives 11111111.

Summary

  • Convert decimal to binary by repeatedly dividing by 2 and collecting remainders.
  • The remainders must be read in reverse to get the correct binary number.
  • Method 1 uses an array; Method 2 uses recursion; Method 3 uses bitwise operators.
  • Always handle 0 as a special case.
  • Example: 13 becomes 1101.

Frequently Asked Questions

How do you convert a decimal number to binary in C?
The standard method is to repeatedly divide the number by 2, save each remainder (0 or 1), and stop when the number becomes 0. The binary result is the remainders read in reverse order — from the last remainder to the first.
Why do we read the remainders in reverse?
Because the first remainder you get is actually the least significant bit (the rightmost digit), while the binary number is written with the most significant bit on the left. Reversing the collected remainders puts the bits in their correct left-to-right positions.
What is 13 in binary?
13 in binary is 1101. You can check it by adding place values: 8 + 4 + 0 + 1 = 13, which matches the bits 1-1-0-1 for the places 8, 4, 2, 1.
Can I convert decimal to binary without an array in C?
Yes. A clean way is recursion: the function calls itself with n / 2 first, then prints n % 2 on the way back, which naturally produces the bits in the correct order without storing them in an array.
How do I handle 0 when converting to binary?
The plain divide-by-2 loop skips 0 because the loop condition n > 0 is false from the start, so it prints nothing. Handle it as a special case by printing 0 directly when the input is 0.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.