In this source code example, we will write a code to binary value to decimal number in the C programming language.
Convert Binary to Decimal in C
In this C program, we will take input from the User or console.
#include <stdio.h>
int main()
{
int remainder, number = 0, decimal_number = 0, temp = 1;
printf("Enter any binary number => ");
scanf("%d", &number);
// Iterate over the number until the end.
while (number > 0)
{
remainder = number % 10;
number = number / 10;
decimal_number += remainder * temp;
temp = temp * 2; // used as power of 2
}
printf("%d", decimal_number);
}
Output:
Enter any binary number => 1110
14
Comments
Post a Comment