Binary to Decimal in C
As we all know, computers cannot read the characters and numbers that human’s input. It can only understand binary numbers, or 0s and 1s. When we enter a decimal number into a computer, various software or compilers assist us in translating the decimal number into its binary form (0s and 1s) so that a computer machine can read human language.
Similarly, when a computer displays some findings, it internally translates the binary data into decimal values using various software programmes that are available on the computer. We will first define binary numbers and decimal numbers before learning how to convert binary to decimal in C.
We convert binary numbers to decimal numbers so that large binary quantities can be interpreted in a human-readable fashion. Binary numbers are base-2 number system numerals that are commonly represented by only two digits or symbols: 0 (zero) and 1 (one), whereas decimal numbers are base-10 number system numbers that are represented by numbers created by digits ranging from 0-9.
Binary number representation is made up of 0s and 1s, which are utilised to encode computer data.
As an example:
- (100)2 in the binary system equals (4)10 in the decimal system
- (1111)2 in the binary system equals (15)10 in the decimal system, and so on.
The above figure which represents the representation of a binary number having increasing values greater from right to left.
C Algorithm for Converting Binary to Decimal:
- Take a user-supplied binary number.
- To preserve the binary number's value in the code, store it in a temporary variable.
- Create a loop (repeat the instructions below) until the temporary variable is larger than zero.
- Divide the temporary variable (binary value) by 10 and save a single binary digit (remainder) from the binary value's right side in a remainder variable.
- Multiply the remaining values by their place values in the base-2 number system, i.e., consecutive powers of 2 (starting with 0).
- Take the sum of the results from each step and store it in a decimal-equivalent variable.
- At each stage, reduce the temperature by a factor of 10 (temp / = 10) and raise the base by a factor of 2 (base *= 2).
- When the loop is completed, the binary number equivalent value is stored in the decimal number variable.
We have certain methods in order to convert from Binary to Numerical values.
Among them some are mentioned below.
C program to for converting Binary into Numerical Value Using WHILE loop:
#include <stdio.h>
int main()
{
int temp1, binary, decimal = 0, base = 1, rem;
printf("Enter the user binary number: ");
// using the user's input binary number
scanf("%d", & binary);
// storing the binary number in a temporary variable for usage in the while loop
temp1 = binary;
// Conversion Algorithm
while (temp1 > 0)
{
// divide the binary number and store the rightmost digit in the remainder variable
rem = temp1 % 10;
decimal= decimal + rem * base;
temp1 /= 10;
base *= 2;
}
printf("\nBinary Number: %d\nDecimal Number Equals to: %d", binary, decimal);
return 0;
}
OUTPUT:
- First, we used the scanf() function to take a binary number from the user.
- We saved the input binary number (1101) in a temporary variable to preserve the binary number's value for future use.
- In the preceding C programme, we used the while loop to implement the conversion procedure.
- We also saved the translated binary number in the decimal Number variable.Finally, in the output, we have printed the decimal equivalent number (13).
C program to for converting Binary into Numerical Value Using FOR loop:
#include <stdio.h>
int main()
{
int binary_number, decimal_number = 0, base1 = 1, rem, temp1;
printf("Enter the User Binary Number: ");
scanf("%d", &binary_number);
for(temp1 = binary_number; temp1 > 0; temp1 = temp1 / 10)
{
rem = temp1 % 10;
decimal_number = decimal_number + rem * base1;
base1 = base1 * 2;
}
printf("Binary Number: %d\n", binary_number);
printf("Decimal Number equals to: %d\n", decimal_number);
return 0;
}
OUTPUT:
- Using the scanf() function, we took a user-supplied binary _number (1101).
- In the above C programme, we used the for loop to apply the conversion method.
- We also saved the translated binary number in a decimal_Number variable.
- Finally, the decimal equivalent number (13) was printed in the output.
C program to for converting Binary into Numerical Value Using FUNCTIONS:
#include<stdio.h>
#include<math.h>
// the conversion algorithm is implemented using a user-defined function.
int binaryToDecimal(int binary)
{
// variable declaration
int sum = 0, temp1, rem, base = 1;
temp1 = binary;
// Conversion Algorithm
while(temp1 > 0)
{
// splitting the binary number and storing the rightmost digit in the variable remaining
rem = temp1 % 10;
sum = sum + (rem * base);
temp1 /= 10;
base *= 2;
}
return sum;
}
int main()
{
int binary, decimal;
printf("Enter the user given binary number: ");
scanf("%d", &binary);
//executing the binaryToDecimal function
// saving the result in the decimalNumber variable
decimal = binaryToDecimal(binary);
printf ("\nBinary number: %d\nDecimal Number: %d", binary, decimal);
return 0;
}
OUTPUT:
- In the main() method, we used the scanf() function to read a binary number (1101) from the user.
- This time, we applied the conversion technique to a user-defined function and used a while loop to implement the algorithm within the function.
- We also saved the converted binary number in a sum variable and passed the value to the main() function.
- Finally, the decimal equivalent number (13) was printed in the output.