Udemy logo

c program to convert decimal to binaryC is probably the most popular programming language in the world today. Most new programmers study C as a first language when they enter the programming world for the first time. C was developed by Dennis Ritchie in the middle of 1970 at Bell Labs. The language was intended to be a system programming language for UNIX systems and its code was derived from an existing language called Basic Combined Programming Language (B or BPCL). However, the language proved to be ideal for developing applications and firmware. Its code was easy to understand and simple to implement, and the language in general proved to be very reliable. Consequently, C went on to become exceedingly popular and was used to make countless applications and operating systems. Today, the C language is used to make graphics packages, word processors, network drivers, spreadsheet software, operating systems, database systems, assemblers, compilers and network drivers. 

In this tutorial, we’re going to take a look at programs that can convert a decimal number to a binary number and vice versa. You need to be familiar with the basics of C to understand this tutorial. If at any time you find yourself stuck on any concept or programming term in this tutorial, you can refer back to our course on the C language for help.

C is reliable, flexible, interactive, modular and portable. The code is efficient – you can perform complex programming tasks with a comparatively small amount of code. C is also a very powerful language and can be used to develop the most complex applications. The language also requires less memory, compared to other languages. You can take this beginners course in C programming to get a jump start.

If you are still wondering whether it’s worth your while to learn C – Yes, it is! C programmers are in high demand, even with the advent of modern programming languages like Java and Ruby. In fact, C is the base of several modern computer languages like C++ and Objective C. Learning C will make it easier for you to understand these modern languages. C is a simple language to learn, even if you are a newbie programmer, and there are countless resources on the internet you can take advantage of. If you want a solid foundation in the C language in little time, you can sign up for this intensive C programming course. We will teach you the basics as well as the advanced techniques a good C programmer should know.

Converting Decimal to Binary- The Theory

Before we begin writing programs to convert decimal numbers to binary, and vice versa, it’s important to understand the mathematical theory behind the process. If you’re confident that you know the theory, you can just skip to the programs ahead.

The Decimal System

If you recall the algebra that you were taught in school, numbers were often sorted into columns:

H(10^2)
T(10^1)
O (10^0)
2
3
4

The number 234 has three digits: 2, 3 and 4. It has 2 hundreds (2*100), 3 tens (3*10) and 4 ones (4*1). The number 2 is placed in the hundred’s column, the number 3 in the ten’s column and the number 4 in the unit place column. The decimal system uses 0 to 9 digits to represent numbers.

You can mathematically write this number as 2*10^2 + 3*10^1 + 4* 10^0 = 234.

All we’ve done is multiply the digit in the hundred-place column by ten raised to the power of two (10^2), the digit in the ten’s column by ten raised to the power of one (10^1) and the digit in the unit’s place column as ten raised to the power of zero (10^0).

The Binary System

So now that you know the decimal system, what’s the binary system? The decimal system dealt with base 10, while the binary system operates in base 2. Everything else is the same. The columns in the binary system are different. In the decimal system, the columns were like this:

H(10^2)|T(10^1)|O (10^0)

In the binary system, however, they are in base 2:

T(2^1)|O (2^0)

In the decimal system, when we run out of numbers (0-9), we start again at 0. But in the binary system, you get only 3 digits (0-2). Every time you run out of digits, you shift the number 1 one column to the left and start with 0 again.

The number 0 will look like this: 0
The number 1 will look like this:  1
The number 2 will look like this: 10
The number 3 will look like this: 11
The number 4 will look like this: 100

Going by this system, the number 234 will be: 11101010. While we used a calculator for this, the method followed to convert this number to the binary form is known as the short division by two method, with remainder. If you’d like to dig deeper into Binary, this course helps explain the details, and show how Binary is the foundation of all digital computing.

C Program to Convert Decimal to Binary

One way to write a program to convert a decimal number to binary would be:

#include
#include
int main()
{
      int d[20];
      int decimalno,i=0;
      printf("Enter the decimal number you want to conver to binary: \n");
      scanf("%d",&decimalno);
      while(decimalno>0)
      {
           d[i]=decimalno%2;
           i++;
           decimalno=decimalno/2;
      }
      for(int j=i-1;j>=0;j--)
      {
            printf("%d",d[j]);
      }
      printf("The binary version of the number you input, %d, is = ",decimalno);
      getch();
 }

Explanation: As you can see, you need to use an array to convert a decimal number to its binary form. We’ve created a variable that can store the value of the decimal number, and then divided it by 2 (short division by 2 method). The remainder of this division will be stored in an array form. Later, we use a loop to reverse the array and then print out the result.

In this example program, we used the loop function and then used it to reverse an array. This is an advanced application of the loop function in C. To learn other such programming tips, you can sign up for this C course, which has a ton of real world examples.

Converting a Binary Number to a Decimal Number

Now let’s write a program that does the opposite of the program above, that is, a program to convert a binary number to a decimal number. The concept for converting a binary number is simpler. The binary number 1100, for example, can be calculated like this:

1*2^3 + 1*2^2 + 1*2*1 + 1*2^0
= 8 + 4 + 2 + 0
= 14

You cannot use the ^ operator in C. So we have to create a function that can take its place. The program will look like this:

#include
#include
int powerfunc(int, int);
int main()
{
    int d[100];
    int n,decimalno=0;
    int j=0,f;
    printf("Enter number of bit places to be calculated \n");
    scanf("%d",&n);
    printf("Enter binary number one digit at a time, press enter after each digit has been entered \n");
    for( int i=0;i=0;i--)
    {
        dec=(d[i]*power(2,j))+decimalno;
        j++;
    }
    printf("The binary ouput of the input decimal number %d is",decimalno);
    getch();
}
int powerfunc(int x, int y)
{
    int power=1;
    int i=1;
    while(i<=y)
    {
        power=power*x;
        i++;
    }
    return power;
}

We created a power function that we referenced inside the program (for the exponentiation ^). The binary number was stored inside an array and then, with the help of a loop and multiplication, we converted it into a decimal number.

We recommend that you test these programs out yourself to understand the concept better. After all, practice makes perfect.  Once you’re ready to move on to more complex programs, you can take this course on creating your own C programs on iOS and experiment with app creation. Who knows, you may end up making the next Angry Birds!

Page Last Updated: April 2014

Top courses in C# (programming language)

Ultimate C# Masterclass for 2023
Krystyna Ślusarczyk
4.7 (986)
Bestseller
C# / .NET Interview Questions with Answers.
Shivprasad Koirala
4.7 (1,241)
Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (40,221)
Bestseller
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.5 (3,520)
Bestseller
Unity RPG Inventory Systems Asset Pack: Behind The Scenes
GameDev.tv Team, Rick Davidson
4.5 (836)
The Unity C# Survival Guide
Jonathan Weinberger, Unity Technologies, GameDevHQ Team
4.7 (1,729)
Advanced Topics in C#
Dmitri Nesteruk
4.5 (545)
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.8 (205)
Bestseller
Design Patterns in C# Made Simple
Zoran Horvat
4.7 (401)
Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.6 (27,163)

More C# (programming language) Courses

C# (programming language) students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo