Introduction :
To convert a decimal (integer) number to binary is a very easy task in programming world. Using the C programming language, you can do it in multiple ways. Here, I convert a decimal (integer) number to binary using “division by 2” method. This program is very useful to work with low level data manipulation.
In this article, I shall show you how to convert a positive decimal number (base 10) to binary (base 2) in C programming language. Here, I create the decimal (integer) to binary converting program for positive integer number. It does not convert negative number to equivalent binary number.
What is binary number :
The numeral system which bases on 2 numeral system is called the binary system. It consists of only two digits such as 0 and 1. All binary numbers are a combination of 0 and 1. Every digit of binary number is called binary digit or bit. The binary system is generally used in electronic devices like computers to process data.
You can see various usages of binary system in many fields such as store data (text, images, audio and video) in binary form, data transfer, binary arithmetic, image and sound processing etc.
About the program :
This C program is developed for converting a decimal (integer) number to a binary number. After running the program, it asks the user to enter any integer number. Then, it converts the input number to its equivalent binary number. At the same time, it displays the result (binary number) on the console screen. If you have entered a negative number, you can see an error message on the screen.
Explanation of the program :
First, include stdio.h library in the program. Then, declare integer variables (deci_num, r, deci_temp, i , j) in the main() function. You have to also declare an array (binary[10]) to store the binary digits. Using printf() function, ask the user to enter a decimal number. You can use scanf() function to get the input and store in “deci_num” variable.
Now, you can use “if else” statement to check the input is negative, zero (0) or valid number. If the input number is negative, it shows an error message otherwise it calculates the binary number of the input. Here, you have to use a while loop which continues until “deci_temp” becomes zero (0). In the while loop, you can use modulus (%) and division (/) operator to find the binary of the input number. Finally, print the output on the screen.
How run the program :
To run the program on your PC, you have to install VS Code. Then, open VS Code and create a new C file (“decimal_to_binary.c”). After that, copy the below code and paste in the file. Now, run the program on your PC to see the output.
Source code of the program :
The following code is the source code of converting decimal (integer) to binary program in C programming language.
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main(void)
{
int deci_num, r, deci_temp, binary[10], i = 0, j = 0;
printf("Enter a decimal number to convert into binary\n");
scanf("%d", &deci_num);
deci_temp = deci_num;
if (deci_num < 0)
{
printf("Please enter a positive number");
}
else if (deci_num == 0)
{
printf("Binary of the decimal number %d is 0", deci_num);
}
else
{
while (deci_temp > 0)
{
r = deci_temp % 2;
deci_temp = deci_temp / 2;
binary[i] = r;
i++;
}
printf("Binary of the decimal number %d is ", deci_num);
for (j = i - 1; j >= 0; j--)
{
printf("%d", binary[j]);
}
}
printf("\n");
return 0;
}
Output of the program :
When you run the program, you can see the following output on your PC.

Conclusion :
Finally, you have learned how to convert positive integer number (decimal) to binary using C programming language in the C programming language. Thank you for visiting my site.