ere you will get program to convert binary to decimal in C.
We can obtain
a decimal number by multiplying each digit of binary number with power
of 2 and adding each multiplication result. The power starts from 0 and
goes to n-1 where n is the total number of digits in binary number.
Below is the program to implement this in C.
Convert Binary to Decimal in C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include<stdio.h>
#include<math.h>
int main()
{
long int i,n,x=0,a;
printf("Enter any binary number: ");
scanf("%ld",&n);
printf("\nThe decimal conversion of %ld is ",n);
for(i=0;n!=0;++i)
{
a=n%10;
x=(a)*(pow(2,i))+x;
n=n/10;
}
printf("%ld",x);
return 0;
}
|
Output
Enter any binary number: 111
The decimal conversion of 111 is 7
0 comments:
Post a Comment