Javatpoint Logo
Javatpoint Logo

Binary to Decimal Number in C

This section will discuss the conversion of Binary numbers to Decimal numbers. Before going to the concept, we need to understand Binary numbers and decimal numbers. As we know, the computer does not understand the words or numbers that humans write or do. Instead, it only understands 0's and 1's. For example, when we type a word or number on a computer, various software or compilers help convert these numbers or words into binary form (0s and 1s bit). So that a computer machine can easily understand these.

Binary to Decimal Number in C

Binary number

A binary number is a number that represents information or data stored in a computer with a combination of 0s and 1s bits. It is also known as the base 2 numeral system because it has two bits, 0s, and 1s. These are binary numbers (0 and 1) 1001, 1010, 1101, 1111, 1010101 etc.

Decimal number

A decimal number is a number that contains 10 digits from 0 to 9. Its base is 10 because it collects 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and represents or makes the whole number using these ten digits.

Algorithm to convert binary to decimal

  1. Take a binary number as the input.
  2. Divide the number by 10 and store the remainder into variable rem.
  3. decimal_num = decimal_num + rem * base;
    Initially, the decimal_num is 0, and the base is 1, where the rem variable stores the remainder of the number.
  4. Divide the quotient of the original number by 10.
  5. Multiply the base by 2.
  6. Print the decimal of the binary number.

Convert binary number into a decimal number using while loop

Let's consider the C program to convert the combination of a binary number (0s and 1s) into the decimal number using a while loop.

program.c

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: As we can see in the above program, it asks a binary number (0s and 1s) from the users to store the number into the variable num. At each iteration, a while loop checks the binary number condition and validates the given number should not be less than 0; otherwise, it exits from the loop.

Following are the iteration of the while loop, as follows:

1st iteration:

rem = 1101 % 10 => 1

decimal_num = 0 + 1 * 1 => 1 (decimal_val = 0, rem = 1, & base = 1)

num = 1101 / 10 => 110

base = 1 * 2 => 2

2nd iteration:

rem = 110 % 10 => 0

decimal_num = 1 + 0 * 2 => 1 (decimal_val = 1, rem = 0, & base = 2)

num = 110 / 10 => 11

base = 2 * 2 => 4

3rd iteration:

rem = 11 % 10 => 1

decimal_num = 1 + 1 * 4 => 5 (decimal_val = 1, rem = 1, & base = 4)

num = 11 / 10 => 1

base = 4 * 2 => 8

4th iteration:

rem = 1 % 10 => 1

decimal_num = 5 + 1 * 8 => 1 (decimal_val = 5, rem = 1, & base = 8)

num = 1 / 10 => 0

base = 8 * 2 => 16

Convert binary number into the decimal number using for loop

Let's consider a program in C language to convert the combination of binary number (0s and 1s) into the decimal number using for loop.

decimal.c

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 consider a program in C language to convert the combination of binary number (0s and 1s) into the decimal number using user defined function.

deci.c

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 consider a program in C language to convert the combination of binary number (0s and 1s) into the decimal number using function and array.

Decimal2.c

Output

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





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA