Program to Convert Binary to Decimal in C
- Program Description:
- What is a Binary Number?
- Program Expected Input and Output:
- Pre-Requisites:
- Program to Convert Binary Number to Decimal Number Algorithm:
- Program to Convert Binary to Decimal in C using while loop:
- Binary to decimal in C – Only Allow Zero’s and One’s:
- Program to Convert Binary to Decimal in C Language using for loop:
- Prompt the user until the valid Input:
- Related Programs:
Program Description:
Write a Program to convert a given number from Binary to Decimal in C programming language. The program should accept a Binary Number (zero(0)’s and 1’s) and convert it to decimal number. The program should also display the error message if the user enters Invalid numbers.
Before going to write the program, we should know what are binary numbers.
What is a Binary Number?
The Binary numeral system is a Base-2 number system and uses only two digits Zero (0)’s and One (1)’s.
We normally use the decimal number system, Which is also called as Base-10 number system.
In the Binary system, each place is a power of two.
How to Convert Binary Number to Decimal Number?
The binary representation of a decimal number 21 is equal to 10101 (binary number)
Here is the step-by-step process of converting a Binary number( 10101) to Decimal Number( 21).
101012 = [ ( 1 ) × 24 ] + [ ( 0 ) × 23 ] + [ ( 1 ) × 22 ] + [ ( 0 ) × 21 ] + [ ( 1 ) × 20 ]
101012 = [ 1 × 16 ] + [ 0 × 8 ] + [ 1 × 4 ] + [ 0 × 2 ] + [ 1 × 1 ]
101012 = 2110
Here is a few binary numbers and their equivalent decimal values
Binary Number | Decimal Number |
---|---|
0 | 0 |
1 | 1 |
10 | 2 |
11 | 3 |
100 | 4 |
101 | 5 |
110 | 6 |
111 | 7 |
1000 | 8 |
1001 | 9 |
1010 | 10 |
1011 | 11 |
1100 | 12 |
1101 | 13 |
1110 | 14 |
1111 | 15 |
Program Expected Input and Output:
Input:
Enter binary number (0's and 1's) : 1010
Output:
binary number : 1010 is equal to decimal number : 10
Pre-Requisites:
We are going to use Arithmetic Operators like Modulus, Division operators, and C Loops. Please go through the following articles to learn more about them.
Program to Convert Binary Number to Decimal Number Algorithm:
- Take the input binary number from the user and store it in num variable
- Check if the num is positive or not using num <= 0 condition. If num is a negative number, display an error message – Error: Please enter valid binary number
- The logic is to get each digit of the binary number num and use the power function to calculate the power of 2 (as it is a binary number) with respect to the digit at the given position.
- We have a variable decimal, which contains the converted number so far, and variable ‘i’, which starts from and is incremented by 1 at each iteration. So we can use i to calculate the power.
- Start the Loop and iterate until the num > 0. At Each Iteration
- We get the last digit of the number ( num) at each iteration. For the first iteration, We get the last digit using the modulus operator – rem = num % 10;
- Then we calculate the rem * pow(2, i);. So At the first Iteration, which is equal to pow(2, 0) and at the second iteration, It will be pow(2, 1), and so on.
- Then add the above result to our decimal variable – decimal = decimal + rem * pow(2, i);
- As we processed the last digit, We need to remove it from the num. To do that, We can use the Division Operation like this – num = num / 10;
- Finally, Increment the variable i. which is used in the calculation of pow function.
- The above loop continues until the num value is greater than the Zero
- Once the loop is terminated, The variable decimal contains the converted number. Display the result on the console.
Program to Convert Binary to Decimal in C using while loop:
We are going to use the pow function from the math.h header file. So don’t forget to add the math.h header file to your program. Here is the prototype of the pow function is
double pow(double x, double y)
If you don’t want to use the inbuilt pow function, you can check the following article, where we wrote a power function manually
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* Program: C Program to convert binary number to decimal number Author: sillycodes.com */ #include<stdio.h> #include<math.h> int main() { // declare variables int num, rem, decimal = 0, i = 0, temp; printf("Enter binary number (0's and 1's) : "); scanf("%d", &num); // Don't allow the negative numbers if(num <= 0) { printf("Error: Please enter valid binary number \n"); return 0; } temp = num; while(num > 0) { // Get the last digit of number rem = num%10; // calculate the decimal decimal = decimal + rem * pow(2, i); // remove the last digit by using division operator. num = num/10; i++; } printf("binary number : %d is equal to decimal number : %d \n", temp, decimal); return 0; } |
Decimal to Binary Conversion Program Output:
We used the pow function to calculate the power value. And pow function is not part of the default C library, So we need to use the -lm option to include the math library functions.
So to Compile the program use the following command ( In Linux based systems)
$ gcc binary-to-decimal.c -lm
The above command generates the executable file ( a.out), Run the executable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ ./a.out Enter binary number (0's and 1's) : 1010 binary number : 1010 is equal to decimal number : 10 $ ./a.out Enter binary number (0's and 1's) : 0111 binary number : 111 is equal to decimal number : 7 $ ./a.out Enter binary number (0's and 1's) : 1111 binary number : 1111 is equal to decimal number : 15 $ $ ./a.out Enter binary number (0's and 1's) : 1011011 binary number : 1011011 is equal to decimal number : 91 $ |
As you can, We are getting the desired results.
Let’s test the negative number case
1 2 3 4 |
$ ./a.out Enter binary number (0's and 1's) : -34 Error: Please enter valid binary number $ |
The program is displaying the Error message on a Negative number
Binary to decimal in C – Only Allow Zero’s and One’s:
What if the user provides digits other than the zeroes and ones? Let’s add another check to our program and accept only the numbers with digits Zero’s and One’s and display an error message if any other digit presents in the given number.
Example:
Input:
Enter binary number (0's and 1's) : 12414001
Output:
1 2 |
Error: Got non-binary Digit (4) Please enter a valid binary number |
We need to make a change to the above program, To check if the digit rem (last digit) is equal to Zero(0) or One(1).
If the rem is not equal to both numbers, Then the provided number num is not a valid binary number. So display the error message and stop the program.
if(rem != 0 && rem != 1)
So Let’s make the above change to our Binary to Decimal number conversion program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/* Program: C Program to convert binary number to decimal number Author: sillycodes.com */ #include<stdio.h> #include<math.h> int main() { // declare variables int num, rem, decimal = 0, i = 0, temp; printf("Enter binary number (0's and 1's) : "); scanf("%d", &num); // Don't allow the negative numbers if(num <= 0) { printf("Error: Please enter valid binary number \n"); return 0; } temp = num; while(num > 0) { // Get the last digit of number rem = num%10; // make sure if 'rem' is only 1 or 0 if(rem != 0 && rem != 1) { // got number other than '0' or '1' printf("Error: Got non-binary Digit (%d) \n", rem); printf("Please enter a valid binary number \n"); return 0; } // calculate the decimal decimal = decimal + rem * pow(2, i); // remove the last digit by using division operator. num = num/10; i++; } printf("binary number : %d is equal to decimal number : %d \n", temp, decimal); return 0; } |
Program Output:
Now, The program should display the error message if num contains any digit other than the binary digits i.e Zero(0)’s and One(1)’s.
📢 Add the -lm option during the program compilation
Let’s start with the Negative test case by providing a number with other digits then Zero(0)’s and One(1)’s.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$ gcc binary-to-decimal.c -lm $ ./a.out Enter binary number (0's and 1's) : 12414001 Error: Got non-binary Digit (4) Please enter a valid binary number $ ./a.out Enter binary number (0's and 1's) : 12001 Error: Got non-binary Digit (2) Please enter a valid binary number $ ./a.out Enter binary number (0's and 1's) : 9109001 Error: Got non-binary Digit (9) Please enter a valid binary number $ |
As you can see from the above example When the user entered 12414001 as the number, Program detected it as not a binary number and displayed the error message Error: Got non-binary Digit (4) and terminated the program.
A similar thing happened for the second and third examples, Where we provided 12001, 9109001. The Program detected and stopped the program.
Let’s also test the positive test cases
1 2 3 4 5 6 7 |
$ ./a.out Enter binary number (0's and 1's) : 101011 binary number : 101011 is equal to decimal number : 43 $ ./a.out Enter binary number (0's and 1's) : 0011101 binary number : 11101 is equal to decimal number : 29 |
As we can see from the above output, We are getting the expected outputs and the program is able to convert the binary number to decimal number correctly.
Program to Convert Binary to Decimal in C Language using for loop:
Let’s write the binary number to decimal number conversion program using the for loop instead of the while loop. The program logic is not going to change. But we are going to use the for loop in this case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* Program: C Program to convert binary number to decimal number using for loop Author: sillycodes.com */ #include<stdio.h> #include<math.h> int main() { // declare variables int num, rem, decimal = 0, i = 0, temp; printf("Enter binary number (0's and 1's) : "); scanf("%d", &num); // Don't allow the negative numbers if(num <= 0) { printf("Error: Please enter valid binary number \n"); return 0; } temp = num; for(; num>0; num = num/10) { // Get the last digit of number rem = num%10; // make sure if 'rem' is only 1 or 0 if(rem != 0 && rem != 1) { // got number other than '0' or '1' printf("Error: Got non-binary Digit (%d) \n", rem); printf("Please enter a valid binary number \n"); return 0; } // calculate the decimal decimal = decimal + rem * pow(2, i); // increment i; i++; } printf("binary number : %d is equal to decimal number : %d \n", temp, decimal); return 0; } |
Program Output:
Test the basic test cases by providing valid binary numbers.
1 2 3 4 5 6 7 8 9 10 |
$ gcc binary-to-decimal.c -lm $ ./a.out Enter binary number (0's and 1's) : 0101101 binary number : 101101 is equal to decimal number : 45 $ ./a.out Enter binary number (0's and 1's) : 1010101010 binary number : 1010101010 is equal to decimal number : 682 $ |
if the user provides a negative number or non-binary digits
1 2 3 4 5 6 7 8 9 |
$ ./a.out Enter binary number (0's and 1's) : 12345101 Error: Got non-binary Digit (5) Please enter a valid binary number $ ./a.out Enter binary number (0's and 1's) : -23 Error: Please enter valid binary number $ |
The program is displaying errors on Negative numbers or Non-binary digits.
Prompt the user until the valid Input:
Use the Goto Statement to prompt the user again for the input in case of Invalid Input.
Modify the above program and the goto statement to jump to the start of the program in case of Invalid Input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* Program: C Program to convert binary number to decimal number using for loop Author: sillycodes.com */ #include<stdio.h> #include<math.h> int main() { START: // goto label // declare variables int num, rem, decimal = 0, i = 0, temp; printf("Enter binary number (0's and 1's) : "); scanf("%d", &num); // Don't allow the negative numbers if(num <= 0) { printf("Error: Please enter valid binary number \n"); // use goto statement to jump to start of the program. goto START; } temp = num; for(; num>0; num = num/10) { // Get the last digit of number rem = num%10; // make sure if 'rem' is only 1 or 0 if(rem != 0 && rem != 1) { // got number other than '0' or '1' printf("Error: Got non-binary Digit (%d) \n", rem); printf("Please enter a valid binary number \n"); // use goto statement to jump to start of the program. goto START; } // calculate the decimal decimal = decimal + rem * pow(2, i); // increment i; i++; } printf("binary number : %d is equal to decimal number : %d \n", temp, decimal); return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ gcc binary-to-decimal.c -lm $ ./a.out Enter binary number (0's and 1's) : 111000111 binary number : 111000111 is equal to decimal number : 455 $ ./a.out Enter binary number (0's and 1's) : -11001 Error: Please enter valid binary number Enter binary number (0's and 1's) : 1110 binary number : 1110 is equal to decimal number : 14 $ ./a.out Enter binary number (0's and 1's) : 123400111 Error: Got non-binary Digit (4) Please enter a valid binary number Enter binary number (0's and 1's) : -354 Error: Please enter valid binary number Enter binary number (0's and 1's) : 10101 binary number : 10101 is equal to decimal number : 21 $ |
As you can see from the above output, The program is displaying the error and allowing the user to provide the input number again.
Related Programs:
- If else practice Programs
- Switch Case Practise Programs
- C program to test Leap year
- C program to Find Area and Perimeter of Rectangle.
- Calculate the Grade of Student.
- Type Conversion Operators on Assignment operator.
- Prime Numbers up to n
- Calculate Sum of Digits Program in C
- Octal to Decimal Number Conversion
6 Responses
[…] C Program to Convert Binary Number to Decimal Number […]
[…] 📢 Related Program: Convert Binary to Decimal using loops […]
[…] C Program to Convert Binary Number to Decimal Number […]
[…] C Program to Convert Binary Number to Decimal Number […]
[…] C Program to Convert Binary Number to Decimal Number […]
[…] C Program to Convert Binary Number to Decimal Number […]