Learn about C program to convert binary to decimal using function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C program to convert binary to decimal using function
Source code:
#include<stdio.h>
#include<math.h>
int convert(int bin);
int main()
{
int binary, decimal;
printf("Enter binary number: ");
scanf("%d",&binary);
decimal = convert(binary);
printf("Decimal value=%d",decimal);
return 0;
}
int convert(int bin)
{
int rem, i=0, dec=0;
while(bin!=0)
{
rem = bin%10;
dec += (rem*pow(2,i));
bin /= 10;
i++;
}
return dec;
}
Output:
Enter binary number: 1010 Decimal value=10
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to convert binary to decimal
C Program to Convert Decimal to Binary using function