Introduction :
Converting binary numbers to decimal numbers is very important in computer science and programming. Binary is the language of computers which base on “base 2” system. It represents all data using two digits such as 0 and 1. When working with low-level data, you have to convert binary to decimal.
Using the C programming language, you can easily convert binary number to equivalent decimal number. In this article, I shall show you how to convert binary number to a decimal number in C programming language.
What is decimal number :
A decimal number is based on “10 number” system. It may be integers and non-integer numbers. It is the more familiar number system that we use in everyday life. There are ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8 and 9) in decimal number system. Every digit in a decimal number has a position that is called place value. It represents by power of 10 that relative to the decimal point.
Due to ease of use, decimal numbers are used in various fields such as mathematics, finance, engineering etc.
About the program :
This is a simple program built by C language that converts a binary number into an equivalent decimal number. At first, the program asks you to enter a binary number. It checks each digit of the input number. If it found any digit is not 0 or 1, you can see an error message on the console screen. If you enter a valid binary number, it displays the equivalent decimal number of the input (binary number) on the console screen.
Explanation of the program :
First, include math.h and stdio.h libraries in the program. Then, declare integer variables such as “binary_num”, “binary_temp”, “dec_num”, “i” and “r” in the main() function. Now, ask the user to enter a binary number using printf() function. To get the input, you can use scanf() function and store in “binary_num” variable. Then, assign the value of “binary_num” to “binary_temp” variable for later usage.
Here, run a while loop until “binary_temp” is less than 0. To calculate the decimal number, you can use modulus (%) and division (/) operators. You have to use pow() function for this calculation. Then, check if the digit is either 0 or 1 using “if else” statement. If each digit of the input is 0 or 1, using printf() function display the result on the screen otherwise display an error message.
How run the program :
To run the program on your PC, you have to install VS Code. Then, open it and create a new file give name as “binary_to_decimal.c”. After that, copy the following code and paste in the file. Now, save the file and run the program on your PC.
Source code of the program :
The following code is the source code of converting a binary number to decimal number in C programming language.
/*Developed by Puskar Jasu*/
#include <math.h>
#include <stdio.h>
int main(void)
{
int binary_num, binary_temp, dec_num = 0, i = 0, r;
printf("Enter a binary number to convert into decimal\n");
scanf("%d", &binary_num);
binary_temp = binary_num;
while (binary_temp > 0)
{
r = binary_temp % 10;
binary_temp = binary_temp / 10;
if (r == 0 || r == 1)
{
dec_num = dec_num + r * pow(2, i);
i++;
}
else
{
printf("You have to enter a binary number\n");
return 1;
}
}
printf("Decimal number of the binary number %d is %d\n", binary_num, dec_num);
return 0;
}
Output of the program :
Now, run the program on your PC and see the output of the program like below image.

Conclusion :
At last, you have learned how to convert binary number to a decimal number in C programming language. You can apply the above code in your own projects. Thank you for visiting my site.