Binary To Decimal Number In C

Binary number

A binary numeral is a numerical system used to store data in a computer, consisting of bits represented by 0s and 1s. It is referred to as the base 2 numeral system due to its utilization of two elements, 0 and 1. Examples of binary numerals include 1001, 1010, 1101, 1111, and 1010101.

Decimal number

A decimal numeral is a numeric representation that comprises ten symbols ranging from 0 to 9. The decimal system is based on 10 digits (0-9) and constructs numerical values by combining these specific symbols.

Algorithm to convert binary to decimal

  • Take a binary number as the input.
  • Divide the number by 10 and store the remainder into variable rem.
  • decimalnum = decimalnum + rem * base; Initially, the decimal_num is 0, and the base is 1, where the rem variable stores the remainder of the number.
  • Divide the quotient of the original number by 10.
  • Multiply the base by 2.
  • Print the decimal of the binary number.
  • Convert binary number into a decimal number using while loop

Let's examine a C program that transforms a binary number (consisting of 0s and 1s) into its decimal equivalent by employing a while loop.

program.c

Example

#include <stdio.h>
#include <conio.h>
void main()
{
	// declaration of variables
	int num, binary_num, decimal_num = 0, base = 1, rem;
	printf (" Enter a binary number with the combination of 0s and 1s \n");
	scanf (" %d", &num); // accept the binary number (0s and 1s)

	binary_num = num; // assign the binary number to the binary_num variable
	
	
	while ( num > 0)
	{
		rem = num % 10; /* divide the binary number by 10 and store the remainder in rem variable. */
		decimal_num = decimal_num + rem * base;
		num = num / 10; // divide the number with quotient
		base = base * 2;
	}

	printf ( " The binary number is %d \t", binary_num); // print the binary number
	printf (" \n The decimal number is %d \t", decimal_num); // print the decimal 
	getch();
}

Output

Output

Enter a binary number with the combination of 0s and 1s
1101
 The binary number is 1101
 The decimal number is 13

Explanations of the code: In the provided code snippet, users are prompted to input a binary number consisting of 0s and 1s, which is then stored in the variable num. Within a loop, a while statement continuously evaluates the binary number to ensure it is not negative; if it is, the loop terminates.

The iterations of the while loop are as outlined below:

1st iteration:

rem = 1101 % 10 => 1

The equation "decimal_num = 0 + 1 * 1" evaluates to 1, where the decimal value is 0, the remainder is 1, and the base is 1.

num = 1101 / 10 => 110

base = 1 * 2 => 2

2nd iteration:

rem = 110 % 10 => 0

The expression "1 + 0 * 2" evaluates to 1 in decimal format, where the decimal value is 1, the remainder is 0, and the base is 2.

num = 110 / 10 => 11

base = 2 * 2 => 4

3rd iteration:

rem = 11 % 10 => 1

The sum of 1 plus 1 multiplied by 4 equals 5 in decimal form, where the decimal value is 1, the remainder is 1, and the base is 4.

num = 11 / 10 => 1

base = 4 * 2 => 8

4th iteration:

rem = 1 % 10 => 1

The decimal value of the expression 5 + 1 * 8 equals 1, with the decimal value being 5, the remainder being 1, and the base being 8.

num = 1 / 10 => 0

base = 8 * 2 => 16

Convert binary number into the decimal number using for loop

Let's explore a C program that converts a binary number (composed of 0s and 1s) into its decimal equivalent by utilizing a for loop.

decimal.c

Example

#include <stdio.h>
#include <conio.h>
#include <math.h> // use math.h header file
void main()
{
	// declaration of local variables i, bin_num, decimal_num = 0, rem;
	int i, bin_num, decimal_num = 0, rem;
	printf (" Enter the binary number with the combination of 0s and 1s \n");
	scanf ("%d", &bin_num); // takes the binary number as the input
	
	printf( " \n The binary number is %d", bin_num); // print the binary number
	for (i = 0; bin_num != 0; ++i)
	{
		rem = bin_num % 10;
		bin_num = bin_num / 10;
		decimal_num = decimal_num + (rem) * ( pow (2, i));
		
	}
	// print the decimal number
	printf ("\n Conversion from binary to decimal number is %d", decimal_num);
	getch();
}

Output

Output

Enter the binary number with the combination of 0s and 1s
10010

 The binary number is 10010
 Conversion from binary to decimal number is 18

Convert binary number into decimal number using function

Let's explore a C program that converts a binary number (composed of 0s and 1s) into its decimal equivalent by utilizing a custom function.

deci.c

Example

#include <stdio.h>
#include <math.h>
#include <conio.h>
int binaryTodecimal(int bin_num);
int main()
{
	// declare the local variable
	int bin_num, dec_num;
	printf (" Enter the binary number (0s and 1s) \n");
	scanf ("%d", &bin_num);
	
	dec_num = binaryTodecimal (bin_num); // call the binaryTodecimal() function
	printf (" Conversion of the binary number to decimal number is %d", dec_num);
}

// use user defined function --- binaryTo decimal function
int binaryTodecimal( int bin_num)
{
	// declaration of variables
	int decimal_num = 0, temp = 0, rem;
	while (bin_num != 0)
	{
		rem = bin_num % 10;
		bin_num = bin_num / 10;
		decimal_num = decimal_num + rem * pow( 2, temp);
		temp++;
	}
	return decimal_num;
}

Output

Output

Enter the binary number (0s and 1s)
11001
 Conversion of the binary number to decimal number is 25

Convert binary number into decimal number using array and function

Let's explore a C program that converts a binary number (consisting of 0s and 1s) into its equivalent decimal number by leveraging functions and arrays.

Decimal2.c

Example

#include <stdio.h>
#include <conio.h>
int binaryTodecimal (char num[])
{
	int i, deci_num, mul = 0;
	for ( deci_num = 0, i = str_length(num) - 1; i >= 0; --i, ++mul)
	{
			deci_num = deci_num + (num[i] - 48) * (1 << mul);
	}
	
	return deci_num;
 }
 
 int str_length( char str[])
 {
 	int i = 0;
 	while (str[i] != '\0')
 		i++;
 	return i;	
 }

int main()
{
	char num[] = "1101";
	int deci_num;
	printf ("\n The binary number is %s", num);
	printf ("\n The decimal number of %s is %d", num, binaryTodecimal(num));
	return 0;
  }

Output

Output

The binary number is 1101
The decimal number of 1101 is 13

Input Required

This code uses input(). Please provide values below: