Binary Number System: The binary number system also known as base 2 number system was invented in 1689 by Gottfried Leibniz it contains two symbol 0 and 1. The base 2 number system is a positional notation with radix of 2. Each of the digit is called as Bit.
Decimal Number System: The decimal number system is one of the most popular number system used in day to day life. It is also known as base 10 number system as it uses digits from 0 to 9
that is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In order to represent number system, we need a base. Decimal numbers are represented in the base 10. Base 10 is the most common base in the world.
Example:
Binary Numbers can be converted to Decimal by the formula –
Decimal Number = ∑ n×2n−1
Here, n is the nth bit of a binary number.
For example – let’s suppose you have to convert 1100111 to decimal
1 | 1 | 0 | 0 | 1 | 1 | 1 |
26 | 25 | 24 | 23 | 22 | 21 | 20 |
1×26 | 1×25 | 0×24 | 0×23 | 1×22 | 1×21 | 1×20 |
Taking the summation from left to right we get –
= 1×26 + 1×25 + 0×24 + 0×23 + 1×22 + 1×21 + 1×20
= 64 + 32 + 0 + 0 + 4 + 2 + 1
= 103
Hence, 1100111 in binary is equivalent to 103 in the decimal number system.
Write a C program that takes a binary number as input and convert it to a decimal number.
Algorithm:
1. Start the program.
2. Take Binary number as input.
3. While the number is greater than 0 divide the number by 10 and take the product of the remainder and 2i, where i is initialized from 0.
4. keep on dividing the quotient by 10 and take the product of the remainder and 2i with i increasing in each iteration by 1.
5. Add up all the products and return the result end.
1. Take a binary number as input.
2. Multiply each digits of the binary number starting from the last with the powers of 2 respectively.
3. Add all the multiplied digits.
4. The total sum gives the decimal number.
There are many approaches to convert a Binary number to Decimal number. Let’s take a detailed look at all the approaches to convert a Binary number to Decimal number in C.
- Convert Binary to Decimal in C using Naive Approach
- Convert Binary to Decimal in C using While Loop
- Convert Binary to Decimal in C using For Loop
- Convert Binary to Decimal in C using Functions
- Convert Binary to Decimal in C using Array
- Convert Binary to Decimal in C using Using an Array to take Input
This approach simply converts the binary number to decimal using the division operation.
Example:
Suppose the input is 10101001, the program should output 169.
Here is source code of the C program to convert binary number to decimal using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to convert the given binary number into decimal
*/
#include <stdio.h>
void main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num); /* maximum five digits */
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
}
1. Take a binary number and store it in the variable num.
2. Initialize the variable decimal_val to zero and variable base to 1.
3. Obtain the remainder and quotient of the binary number. Store the remainder in the variable rem and override the variable num with quotient.
4. Multiply rem with variable base. Increment the variable decimal_val with this new value.
5. Increment the variable base by 2.
6. Repeat the steps 3, 4 and 5 with the quotient obtained until quotient becomes zero.
7. Print the variable decimal_val as output.
Time complexity: O(log10 n)
The time complexity of the above program is O(log10 n), where n is the number of binary bits.
Space Complexity: O(1)
The Space Complexity of the above program is O(1) as no additional extra space is used.
In this case, we are entering the binary number “10101001” as input.
Enter a binary number(1s and 0s) 10101001 The Binary number is = 10101001 Its decimal equivalent is = 169
In this approach, we convert the binary number to decimal using the division operation.
Example:
Suppose the input is 1100111, the program should output 103.
Here is source code of the C program to convert binary number to decimal using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to convert the given binary number into decimal
*/
#include <stdio.h>
#include <math.h>
int main()
{
int number;
int sum = 0;
int i = 0;
printf("Enter the number: ");
scanf("%d", &number);
while (number > 0)
{
sum += (number % 10) * pow(2, i);
number /= 10;
i++;
}
printf("The Decimal Value of Binary Number is %d", sum);
return 0;
}
1. Initially declare a variable named number to take binary input.
2. Then run a while loop while the number is greater than 0 and keep on dividing the number by 10 also declare the sum variable and initialize it to 0.
3. It takes the sum of the product of remainder and ith power of 2 in each iteration where i is in range (0 to n-1), where n is the total number of binary bits.
4. Print the sum.
Note: pow(x,y) is a special function Present in math.h header file in C. It takes 2 input x and y and prints the result in the form xy.
Time complexity: O(log10 n)
The time complexity of the above program is O(log10 n), where n is the number of binary bits.
Note: As with each iteration, we divide the number by 10, so let’s say the number n after k iterations will be like n/10k = 1, which on simplifying be K = log10 n.
Space Complexity: O(1)
The Space Complexity of the above program is O(1) as no additional extra space is used.
In this case, we are entering the binary number “1100111” as input.
Enter a binary number(1s and 0s) 10101001 The Binary number is = 1100111 Its decimal equivalent is = 103
The basic structure of the code is similar to the while loop. The advantage of using for loop is here, the user declares/initializes the variable in a single line. So, it is relatively compact than while loop.
Example:
Suppose the input is 1101, the program should output 13.
Here is source code of the C program to convert binary number to decimal using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to convert the given binary number into decimal using for loop
*/
#include <stdio.h>
#include <math.h>
int main()
{
int number;
int sum = 0;
printf("Enter the number: ");
scanf("%d", &number);
for (int i = 0; number != 0; i++)
{
sum += (number % 10) * pow(2, i);
number /= 10;
}
printf("The Decimal Value of Binary Number is %d", sum);
return 0;
}
Declare a for loop with i initialized to 0 and the second condition be for binary number must not equal to zero.
That is -> for (int i = 0; number != 0; i++)
On each iteration keep decreasing the number by a factor of 10 and increasing i by 1 and storing the sum of each element in a user defined variable named sum (initialized at 0) and at last when the entire n bit is computed print the sum.
Time complexity: O(log10 n)
The time complexity is the same as that of while loop i.e. O(log10 n), where n is the number of binary bits.
Space Complexity: O(1)
The Space Complexity of the above program is O(1) as no additional extra space is used.
In this case, we are entering the binary number “1101” as input.
Enter the number: 1101 The Decimal Value of Binary Number is 13
In this approach, we convert the binary number to decimal using function. Functions enhance the code reusability and prevent users from violating the DRY principle which is don’t repeat yourself.
Example:
Suppose the input is 101, the program should output 5.
Here is source code of the C program to convert binary number to decimal using functions. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to convert the given binary number into decimal using functions
*/
#include <stdio.h>
#include <math.h>
//Function to convert Binary to Decimal
int Binary_to_Decimal(int number)
{
int sum = 0; int i = 0;
while (number > 0)
{
sum += (number % 10) * pow(2, i);
number /= 10;
i++;
}
return sum;
}
//Driver Code
int main()
{
int number;
printf("Enter the number: ");
scanf("%d", &number);
printf("The Decimal Value of Binary Number is %d", Binary_to_Decimal(number));
return 0;
}
Created a function named Binary_to _Decimal which takes a binary number as input. Further, it keeps on dividing the binary number by 10 to record the remainder and store the product of the remainder and ith power of 2 in each iteration where i is in the range (0 to n-1) where n is the total number of binary bits in the variable named sum initialized at 0 and after taking summation of all bits it prints the sum that is,
Sum = ∑ n×2n−1
In the Driver program, the user declares a variable named number in which he takes binary input and then calls the Binary_to _Decimal function passing the input and printing the result.
Time complexity: O(log10 n)
The time complexity is the same as that of while loop i.e. O(log10 n), where n is the number of binary bits. As in every iteration, we divide the number by 10, so for n size number log10 n divisions be needed.
Space Complexity: O(1)
The Space Complexity of the above program is O(1) as no additional extra space is used.
In this case, we are entering the binary number “101” as input.
Enter the number: 101 The Decimal Value of Binary Number is 5
In this approach, we convert the binary number to decimal using array. This approach is not an optimized approach as it uses an array, which causes extra space complexity of O(n).
Example:
Suppose the input is 1111, the program should output 15.
Here is source code of the C program to convert binary number to decimal using array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to convert the given binary number into decimal using array
*/
#include <stdio.h>
#include <math.h>
int main()
{
int number;
int arr[100]; //declaration of array
int sum = 0;
for (int i = 0; i < 100; i++)
{
arr[i] = 0; //intializing array with 0
}
int i = 0;
printf("Enter the number: ");
scanf("%d", &number);
while (number > 0)
{
//storing ith index as (i+1)th bit*2^(i);
arr[i] = (number % 10) * pow(2, i);
number /= 10;
i++;
}
for (int i = 0; i < 100; i++)
{
//summation of all the individual (i+1)th bit*2^(i)
sum += arr[i];
}
printf("The Decimal Value of Binary Number is %d", sum);
return 0;
}
Declare the array of size equals to the number of bits in binary digit and initialize it to 0, now moving from least significant bit to most significant bit calculate the product of ith bit and 2(i-1) and store the value in array index starting from 0. (keep in mind the zero-based indexing of array)
Declare the sum variable initialize it to 0 and using for loop compute the sum of all individual components, the sum obtained is the required decimal value.
Time complexity: O(log10 n)
The time complexity is O(log10 n) as the basic approach does not change we are still dividing the number by 10 in order to obtain the bits.
Space Complexity: O(n)
The space complexity is O(n) due to the extra auxiliary array needed to store the intermediate result.
In this case, we are entering the binary number “1111” as input.
Enter the number: 1111 The Decimal Value of Binary Number is 15
In this approach, we convert the binary number to decimal using an array to take input. This approach is not an optimized approach as it uses an array, which causes extra space complexity of O(n).
Example:
Input:
Enter the size of binary number: 2
Enter the binary number: 1 1
Output:
The Decimal Value of Binary Number is 3
Here is source code of the C program to convert binary number to decimal using array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to convert the given binary number into decimal using array
*/
#include <stdio.h>
#include <math.h>
int main()
{
int size;
int sum = 0;
printf("Enter the size of binary number: ");
scanf("%d", &size);
int binary[size];
printf("Enter the binary number: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &binary[i]);
}
for (int i = 0; i < size; i++)
{
sum += binary[size - i - 1] * pow(2, i);
}
printf("The Decimal Value of Binary Number is %d", sum);
return 0;
}
Declare an array to store the binary input with the size of the array same as the number of binary bits and then starting from i = 0 take the product of array[size-i-1] (least significant bit) and 2i and add it to the sum variable declared and initialized to 0 using for loop as shown in the above code in a user-defined variable named sum initialized at 0.
When the entire array is traversed and computed print the value of the sum which is the desired decimal value of the given binary input.
Time complexity: O(n)
The time complexity is O(n), where n is the number of bits in binary number because the loop will run at max n times.
Space Complexity: O(n)
The space complexity is O(n) due to the declaration of an array of size n.
In this case, we are entering the size of binary number is 2 and binary number “11” as input.
Enter the size of binary number: 2 Enter the binary number: 1 1 The Decimal Value of Binary Number is 3
Applications:
- Binary number systems are the backbone of modern-day computers and electronics be it binary adders, multiplexers, logic gates, etc.
- Whereas the Decimal number system constitutes modern-day mathematics and counting.
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
If you find any mistake above, kindly email to [email protected]- Apply for Computer Science Internship
- Check C Books
- Practice Computer Science MCQs
- Practice BCA MCQs
- Check Computer Science Books