C Program to Convert Binary to Decimal

«
»

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.

Problem Description

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.

Problem Solution

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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.

Method 1: (Naive Approach)

This approach simply converts the binary number to decimal using the division operation.

Example:
Suppose the input is 10101001, the program should output 169.

Program/Source Code

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.

  1. /*
  2.  * C program to convert the given binary number into decimal
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     int  num, binary_val, decimal_val = 0, base = 1, rem;
  9.  
  10.     printf("Enter a binary number(1s and 0s) \n");
  11.     scanf("%d", &num); /* maximum five digits */
  12.     binary_val = num;
  13.     while (num > 0)
  14.     {
  15.         rem = num % 10;
  16.         decimal_val = decimal_val + rem * base;
  17.         num = num / 10 ;
  18.         base = base * 2;
  19.     }
  20.     printf("The Binary number is = %d \n", binary_val);
  21.     printf("Its decimal equivalent is = %d \n", decimal_val);
  22. }
Program Explanation

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.

advertisement
Runtime Test Cases

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

Method 2: (While Loop)

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.

advertisement
Program/Source Code

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.

  1. /*
  2.  * C program to convert the given binary number into decimal
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6. int main()
  7. {
  8.     int number;
  9.     int sum = 0;
  10.     int i = 0;
  11.     printf("Enter the number: ");
  12.     scanf("%d", &number);
  13.     while (number > 0)
  14.     {
  15.         sum += (number % 10) * pow(2, i);
  16.         number /= 10;
  17.         i++;
  18.     }
  19.     printf("The Decimal Value of Binary Number is %d", sum);
  20.     return 0;
  21. }
Program Explanation

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.

Runtime Test Cases

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

Method 3: (For Loop)

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.

Program/Source Code

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.

  1. /*
  2.  * C program to convert the given binary number into decimal using for loop
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6. int main()
  7. {
  8.     int number;
  9.     int sum = 0;
  10.     printf("Enter the number: ");
  11.     scanf("%d", &number);
  12.     for (int i = 0; number != 0; i++)
  13.     {
  14.         sum += (number % 10) * pow(2, i);
  15.         number /= 10;
  16.     }
  17.     printf("The Decimal Value of Binary Number is %d", sum);
  18.     return 0;
  19. }
Program Explanation

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.

Runtime Test Cases

In this case, we are entering the binary number “1101” as input.

 
Enter the number: 1101
The Decimal Value of Binary Number is 13

Method 4: (Using Functions)

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.

Program/Source Code

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.

  1. /*
  2.  * C program to convert the given binary number into decimal using functions
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. //Function to convert Binary to Decimal
  8. int Binary_to_Decimal(int number)
  9. {
  10.     int sum = 0; int i = 0;
  11.     while (number > 0)
  12.     {
  13.         sum += (number % 10) * pow(2, i);
  14.         number /= 10;
  15.         i++;
  16.     }
  17.     return sum;
  18. }
  19.  
  20. //Driver Code
  21. int main()
  22. {
  23.     int number;
  24.  
  25.     printf("Enter the number: ");
  26.     scanf("%d", &number);
  27.  
  28.     printf("The Decimal Value of Binary Number is %d", Binary_to_Decimal(number));
  29.     return 0;
  30. }
Program Explanation

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.

Runtime Test Cases

In this case, we are entering the binary number “101” as input.

 
Enter the number: 101
The Decimal Value of Binary Number is 5

Method 5: (While Array)

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.

Program/Source Code

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.

  1. /*
  2.  * C program to convert the given binary number into decimal using array
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6. int main()
  7. {
  8.     int number;
  9.     int arr[100]; //declaration of array
  10.     int sum = 0;
  11.     for (int i = 0; i < 100; i++)
  12.     {
  13.         arr[i] = 0; //intializing array with 0
  14.     }
  15.  
  16.     int i = 0;
  17.     printf("Enter the number: ");
  18.     scanf("%d", &number);
  19.     while (number > 0)
  20.     {
  21.         //storing ith index as (i+1)th bit*2^(i);
  22.         arr[i] = (number % 10) * pow(2, i); 
  23.         number /= 10;
  24.         i++;
  25.     }
  26.     for (int i = 0; i < 100; i++)
  27.     {
  28.         //summation of all the individual (i+1)th bit*2^(i)
  29.         sum += arr[i]; 
  30.     }
  31.  
  32.     printf("The Decimal Value of Binary Number is %d", sum);
  33.     return 0;
  34. }
Program Explanation

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(1)
The space complexity is O(n) due to the extra auxiliary array needed to store the intermediate result.

Runtime Test Cases

In this case, we are entering the binary number “1111” as input.

 
Enter the number: 1111
The Decimal Value of Binary Number is 15

Method 6: (Using an Array to Take Input)

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

Program/Source Code

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.

  1. /*
  2.  * C program to convert the given binary number into decimal using array
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6. int main()
  7. {
  8.     int size;
  9.     int sum = 0;
  10.  
  11.     printf("Enter the size of binary number: ");
  12.     scanf("%d", &size);
  13.  
  14.     int binary[size];
  15.     printf("Enter the binary number: ");
  16.  
  17.     for (int i = 0; i < size; i++)
  18.     {
  19.         scanf("%d", &binary[i]);
  20.     }
  21.  
  22.     for (int i = 0; i < size; i++)
  23.     {
  24.         sum += binary[size - i - 1] * pow(2, i);
  25.     }
  26.  
  27.     printf("The Decimal Value of Binary Number is %d", sum);
  28.     return 0;
  29. }
Program Explanation

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.

Runtime Test Cases

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”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.