February 14, 2011

C Program for Base Conversion from Binary to Decimal

The binary (base twonumeral system has two possible values, often represented as 0 or 1, for each place-value. In contrast, the decimal(base tennumeral system has ten possible values (0,1,2,3,4,5,6,7,8, or 9) for each place-value.

To avoid confusion while using different numeral systems, the base of each individual number may be specified by writing it as a subscript of the number. For example, the binary number 10011100 may be specified as "base two" by writing it as 100111002. The decimal number 156 may be written as 15610 and read as "one hundred fifty-six, base ten".


Code:

//C Program for Base Conversion from Binary to Decimal
#include<stdio.h>
#include<conio.h>
int power (int);
void main()
{
long int n;
int x,s=0,i=0,flag=1;
clrscr();
printf("Input a Binary Number\n\n");
scanf("%ld",&n);
while(flag==1)
{
x=n%10;
s=s+x*power(i);
i=i+1;
n=n/10;
if(n==0)
flag=0;
}
printf("\nDecimal Equivalent = %d",s);
getch();
}
power(int i)
{
int j,p=1;
for(j=1;j<=i;j++)
p = p*2;
return(p);
}

Hope you liked my posts, give comments if you have any doubts.. :)

Related Posts :



Categories:

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Related Posts with Thumbnails