C Program to convert a given decimal number into its binary equivalent

This C program converts a given decimal number into its binary equivalent. A decimal number uses base 10 (digits 0–9), while a binary number uses base 2 (only 0 and 1) — the language computers actually work in. Converting between them is one of the first things every programmer learns. In this tutorial we walk through the algorithm step by step, give you clean working code, and show a faster bitwise alternative.

The Algorithm

To convert a decimal number to binary, repeatedly divide the number by 2 and record the remainder each time:

  1. Divide the number by 2 and store the remainder (0 or 1).
  2. Replace the number with the quotient.
  3. Repeat until the number becomes 0.
  4. The binary equivalent is the remainders read in reverse order (bottom to top).

For example, for 13: 13/2 → r1, 6/2 → r0, 3/2 → r1, 1/2 → r1. Reading bottom-up gives 1101.

The Program

#include <stdio.h>

int main(void)
{
    int num, original;
    int binary[32];
    int count = 0;

    printf("Enter a decimal number : ");
    if (scanf("%d", &num) != 1 || num < 0) {
        printf("Please enter a non-negative integer.\n");
        return 1;
    }

    original = num;

    /* Handle 0 explicitly so we still print a digit. */
    if (num == 0) {
        binary[count++] = 0;
    }

    /* Collect remainders. */
    while (num > 0) {
        binary[count] = num % 2;
        num = num / 2;
        count++;
    }

    /* Print the remainders in reverse order. */
    printf("The binary equivalent of %d is : ", original);
    for (int i = count - 1; i >= 0; i--)
        printf("%d", binary[i]);
    printf("\n");

    return 0;
}

How the Program Works

  • We read the decimal number with scanf and reject negative input so the loop is well defined.
  • num % 2 gives the current binary digit (the remainder), and num / 2 moves to the next position. Each digit is stored in the binary[] array.
  • The remainders come out least-significant first, so we print the array backwards with the final for loop to get the correct binary string.
  • The if (num == 0) guard makes sure the program prints 0 instead of nothing when the user enters zero.

Sample Output

Enter a decimal number : 13
The binary equivalent of 13 is : 1101

Enter a decimal number : 100
The binary equivalent of 100 is : 1100100

Faster Alternative: Bitwise Operators

Because computers store integers in binary already, you can read the bits directly with the right-shift (>>) and bitwise-AND (&) operators instead of dividing:

for (int i = 31; i >= 0; i--)
    printf("%d", (num >> i) & 1);

This prints all 32 bits (including leading zeros). It is faster because shifting and masking are single CPU instructions, while integer division is more expensive.

Time Complexity

Method Time Complexity Notes
Division by 2 O(log₂ n) One step per binary digit
Bitwise shift O(1) per bit, fixed 32 iterations Prints leading zeros

For a thorough grounding in number systems, bitwise operators, and how C represents integers, The C Programming Language by Kernighan and Ritchie is the classic reference — find it on Amazon.

This post contains affiliate links. If you buy through them, we may earn a small commission at no extra cost to you.

Related C Programs

Want to try this without installing anything? Paste the code into one of the best online C compilers and run it instantly. For a permanent setup, see our guide to a complete C development environment.

C Program To Find The Largest Of 3 Numbers

C Program To Find The Biggest(Largest) Of 3 Numbers. In this program, We find the biggest of three number using simple else-if ladder. Read more about C Programming Language .
 [gist id=”7019035″ show_line_numbers="1" highlight="26-32"]

Read more Similar C Programs
Array In C
Number System

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,  our page!

Share this program with your Facebook friends now! by liking it

Like to get updates right inside your feed reader? Grab our feed!
(c) www.c-program-example.com

C Program to find the array c such that c[i] = a[i ]+ b[n-1-i]

Example programs to solve the problems of Arrays in C,
a and b are two integers arrays each with n elements. C program to find the array c such that c[i]=a[i]+b[n-1-i]. Read more about C Programming Language .

[gist id=”7011382″]
Read more Similar C Programs
Array In C

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

 our page!

Share this program with your Facebook friends now! by liking it

 (you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to classify the triangle as equilateral, isosceles and scalene

C Program to classify the triangle as equilateral, isosceles and scalene.
Equilateral triangle means all three side and angles are equal(i.e. 60 degree).
Isosceles triangle means any two side and angles are equal.
Scalene triangle means , any sides and any angles are not equal.
Read more about C Programming Language .

Read more Similar C Programs
Learn C Programming

Simple C Programs

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to compute the area of an isosceles triangle

C Program to compute the area of an isosceles triangle. Isosceles Triangle is triangle, in which any two sides and angles are equal. Here, We check the given triangle is Isosceles or not also and print the suitable messages. Read more about C Programming Language .
Read more Similar C Programs
Learn C Programming

Simple C Programs

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

–>

(c) www.c-program-example.com

Print “Hello World” Without Using Semicolon

How to Write a “Hello World” C program without using semicolon?
Asked in various interviews…
Read more about C Programming Language .

Read more Similar C Programs
C Interview Questions

Number Theory 
C Strings

You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program
You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!
Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)
Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com