Octal To Binary Program In C
Implementation of this algorithm is given below −
#include <stdio.h>
int string_length(char s[])
{
int i=0;
while(s[i]!='\0')
i++;
return i;
}
void string_reverse(char s[])
{
char *t;
for(t=s+string_length(s)-1;s<t;++s,--t) {
char temp=*s; *s=*t; *t=temp;
}
}
int binary2decimal(char n[])
{
int i,decimal,mul=0;
for(decimal=0,i=string_length(n)-1;i>=0;--i,++mul)
decimal+=(n[i]-48)*(1<<mul);
return decimal;
}
int main()
{ char n[10],bits[100];
int i,j,d;
printf("Enter a octal number : ");
scanf("%s",n);
string_reverse(n);
for(i=j=0;n[i]!='\0';++i)
for(d=n[i]-48;d!=0;d/=2)
bits[j++]=d%2+48;
bits[j]='\0';
string_reverse(bits);
printf("Binary equivalent is : %s\n",bits);
return 0;
}
Output
Output of the program should be −
24 is even 31 is odd
Advertisements