- Related Questions & Answers
- C How To Convert Binary To Decimal
- Convert Decimal To Binary Algorithm
- C Code Convert Binary To Decimal
- Selected Reading
C Programming & Data Structures: C Program For Binary to Decimal Conversion.Topics discussed:1) Binary to decimal conversion.2) C program to convert binary n. Binary to Decimal Conversion; then refer to Binary to Decimal conversion step by step process. Now let's move on to the program. Binary to Decimal in C. To convert binary number to decimal number in C programming, you have to ask from user to enter the binary number and then convert it into decimal number as shown in the following program.
Given with a binary number as an input, the task is to convert the given binary number into a decimal number.
Decimal number in computers is represented with base 10 and binary number is represented with base 2 as it has only two binary digits 0 and 1 whereas decimal numbers can be any numeric digit starting from 0 – 9.
To convert a binary number into a decimal number we will extract digits starting from right to left through a remainder and then multiply it with the power of 2 starting from 0 and will be increased by 1 till the (number of digits) – 1 and with that keep adding the multiplied values to obtain final decimal value.
Given below is the pictorial representation of converting a binary number into a decimal number.
Example
Algorithm
Example
Output
IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT
Summary: In this tutorial, we will learn different ways to convert a decimal number into its corresponding binary representation in C programming language.
Example:
12345Input:1111Output:15Input:101Output:5Method 1: Using While Loop
This is the standard way of converting a binary number to its corresponding decimal representation.
We multiply each binary digit with their corresponding exponential value of 2 (i.e. 2position_from_right -1) and sum them up.
In our program, we use a while loop to extract each binary digit, multiply them with 2x and sum them.
123456789101112131415161718192021222324252627#include <stdio.h>intmain(){intbin,i=0,decimal=0,digit;printf('Enter a Binary number: n');scanf('%d',&bin);while(bin!=0){//extract the digitdigit=bin%10;//multiply the digit with the 2x and add to the decimaldecimal=decimal+pow(2,C How To Convert Binary To Decimal
i)*digit;//remove the digit from the binarybin=bin/10;//increment the exponent poweri=i+1;}printf('Decimal: %d',decimal);return0;}Output:
Enter a Binary number:1011
Decimal: 11
Method 2: Using Recursion
We can also convert a binary number into a decimal using recursion.
In this method, we use the same approach as the above program but implement it in a recursive way.
123456789101112131415161718192021222324252627282930313233#include <stdio.h>//recursive function to convert binary to decimal intbinToDec(intbin,inti){/* *i represents the nth recursive call as well as *nth digit from right, so it can be used as an exponent *///base conditionif(bin0||bin1)returnpow(2,i)*bin;//extract binary digit, then multiply 2x and-//sum with the next recusive callConvert Decimal To Binary Algorithm
intdigit=bin%10;return(pow(2,i)*digit)+binToDec(bin/10,++i);}intmain(){C Code Convert Binary To Decimal
intbin,decimal;printf('Enter a Binary number: n');scanf('%d',&bin);decimal=binToDec(bin,0);printf('Decimal: %d',decimal);return0;}Output:
Enter a Binary number:101
Decimal: 5
In each recursive call, we extract a digit from the binary number and multiply it with its corresponding value of 2 raised to the power its position in the original binary number from the right.
Finally, we sum the multiplication result with the next recursive call result.
Note: Is it important to eliminate the last digit from binary on every recursive call.
In this programming example, we learned to convert a binary number into decimal in C. If you have any doubts or suggestions then please comment below.