C Program to Convert Binary Number to Decimal
C
// C program to convert binary to decimal #include <stdio.h> // Function to convert binary to decimal int binaryToDecimal( int n) { int num = n; int dec_value = 0; // Initializing base value to 1, i.e 2^0 int base = 1; int temp = num; // Extracting the last digit of the binary number while (temp) { int last_digit = temp % 10; // Removing the last digit from the binary number temp = temp / 10; // Multiplying the last digit with the base value // and adding it to the decimal value dec_value += last_digit * base; // Updating the base value by multiplying it by 2 base = base * 2; } // Returning the decimal value return dec_value; } // Driver program int main() { int num = 10101001; printf ( "%d" , binaryToDecimal(num)); } |
169
Complexity Analysis
- Time complexity: O( log n)
- Auxiliary Space: O(1)
Note: In the above program, we represented a binary number as integer value with base 10 as binary numbers are not directly supported by C language.
Refer to the complete article Program for Binary To Decimal Conversion for more details!
Related Articles
Convert Binary to Decimal in C
In this article, we will learn how to write a C program to convert the given binary number into an equivalent decimal number. Binary numbers are expressed in base 2 ( 0, 1 ) and decimal numbers are expressed in base 10 ( 0-9 ).