**Binary to decimal program in c** is program to convert binary to decimal in c.

In this tutorial we are going to see what is decimal number, What is binary number, how to convert binary to decimal, c program to convert binary to decimal.

**What is decimal number**

A numbers we use in everyday life from 0 to 9 are the decimal numbers

**What is binary number**

A number expressed in base 2 represented in 0 and 1 are binary numbers.

**How to convert binary to decimal**

Binary to decimal conversion : write down the powers of two start from right to left. Calculate power of 2 for each digit. Write down the final value of each power of two. Add the final values. This final result is our decimal number.

**Binary to decimal formula : **

Decimal number = Add the power of 2 for each digits in binary number.

**Binary to decimal example **

**Concept you need to know from c**

1. While loop in c

2.Assignment and arithmetic operator in c

**Binary to decimal algorithm**

Binary to decimal in c

1. Start

2. Declare variable

3. Find power of two for each digit start from right to left

4. Calculate power of two

5. Add final values

6. Print decimal number

7. End

## Binary to decimal program in c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> #include<math.h> main() { int dec=0,temp,i,inc=0,binary; printf("Enter binary number to convert to binary number\n"); scanf("%d",&binary); temp=binary; while(temp!=0) { i=temp%10; dec=dec+(i*pow(2,inc)); temp=temp/10; inc++; } printf("Decimal number of %d is %d\n",binary,dec); } |

**Output :**

Binary to decimal in c

**Explanation : **

**1. Start writing with header files**

**2. Write main function**

**3. Declare variable**

dec => to store decimal number

binary => to store binary number

i, inc , temp => temporary variable

**4. Take input binary number**

scanf(“%d”,&binary);

=> binary = 101

**5. Copy binary number to temporary variable**

=> temp= 101

**6. Main logic begins**

while(temp!=0)

**while loop iteration 1 :**

101 != 0 condition true loop executed

i=temp%10 => i = 1

dec=dec+(i*pow(2,inc)) => dec = 1

temp=temp/10 => temp = 10

inc++ => inc = 1

**while loop iteration 2 :**

10 != 0 condition true loop executed

i=temp%10 => i = 0

dec=dec+(i*pow(2,inc)) => dec = 1

temp=temp/10 => temp = 1

inc++ => inc = 2

**while loop iteration 3 :**

1 != 0 condition true loop executed

i=temp%10 => i = 1

dec=dec+(i*pow(2,inc)) => dec = 5

temp=temp/10 => temp = 0

inc++ => inc = 3

**while loop iteration 4 :**

0 != 0 condition false and loop execution stops

**7. Print decimal number**

printf(“Decimal number of %d is %d\n”,binary,dec);

=> Decimal number of 101 is 5

**Recommended posts**

## Leave a Reply