- Write a C program to convert binary number to decimal number system.
- Wap in C to convert a base 2 number to a base 10 number.

#### Required Knowledge

This program converts a **binary number**( base 2) to **decimal number** (base 10).
**Binary number system** is a base 2 number system using digits 0 and 1 whereas **Decimal number system** is base 10 and using digits from 0 to 9. Given a binary number as input from user convert it to decimal number.

##### For Example

00000111 in Binary is equivalent to 7 in Decimal number system.**Algorithm to convert Binary to Decimal number**

- We multiply each binary digit with 2
^{i}and add them, where i is the position of the binary digit(starting from 0) from right side. Least significant digit is at position 0.

Let's convert 0101 binary number to decimal number

Decimal number = 0*2^{3}+ 1*2^{2}+ 0*2^{1}+ 1*2^{0}= 0 + 4 + 0 + 1 = 5

## C program to convert a decimal number to octal number

#include <stdio.h> #include <math.h> int main() { long binaryNumber, decimalNumber=0; int position=0, digit; printf("Enter a Binary Number\n"); scanf("%ld", &binaryNumber); while(binaryNumber!=0) { /* get the least significant digit of binary number */ digit = binaryNumber%10; decimalNumber += digit*pow(2, position); position++; binaryNumber /= 10; } printf("Decimal Number : %ld", decimalNumber); return 0; }

**Program Output**

Enter a Binary Number 00000111 Decimal Number : 7

Enter a Binary Number 00010000 Decimal Number : 16

**Related Topics**