This is a C program to convert binary number to decimal.
This program takes a binary number as input and converts it into decimal number.
1. Take a binary number as input.
2. Multiply each digits of the binary number starting from the last with the powers of 2 respectively.
3. Add all the multiplied digits.
4. The total sum gives the decimal number.
Here is source code of the C program to convert binary number to decimal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to convert the given binary number into decimal
int num, binary_val, decimal_val = 0, base = 1, rem;
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num); /* maximum five digits */
binary_val = num;
while (num > 0)
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
1. Take a binary number and store it in the variable num.
2. Initialize the variable decimal_val to zero and variable base to 1.
3. Obtain the remainder and quotient of the binary number. Store the remainder in the variable rem and override the variable num with quotient.
4. Multiply rem with variable base. Increment the variable decimal_val with this new value.
5. Increment the variable base by 2.
6. Repeat the steps 3, 4 and 5 with the quotient obtained until quotient becomes zero.
7. Print the variable decimal_val as output.
Enter a binary number(1s and 0s) 10101001 The Binary number is = 10101001 Its decimal equivalent is = 169
Sanfoundry Global Education & Learning Series – 1000 C Programs.