Monday, 26 December 2011

C program to convert a binary number to decimal,octal and hexadecimal number and back



#include<stdio.h>
main()
{
int n,z,r,b,b1,b2,d,d1,o,o1,h,i,j,y1[10]={0},y2[10]={0},y3[10]={0},y4[10]={0};
char a,x[20];
printf("\tConversions\n1.Binary to Decimal\n2.Decimal to Binary\n3.Binary to Octal\n4.Octal to Binary\n5.Binary to Hexadecimal\n6.Hexadecimal to Binary\n7.Exit");
while(1)
{
printf("\nEnter you choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("Enter the Binary number\n");
scanf("%d",&b);
z=1,d=0;
while(b>0)
{
r=b%10;
if(r>1)
{
printf("Invalid Entry");
exit(0);
}
d=d+(r*z);
z=z*2;
b=b/10;
}
printf("Decimal equivalent = %d",d);
break;

case 2:
printf("Enter the Decimal number\n");
scanf("%d",&d1);
j=0;
while(d1>0)
{
r=d1%2;
y1[j]=r;
j++;
d1=d1/2;
}
printf("Binary equivalent =");
for(i=j-1;i>=0;i--)
printf("%d",y1[i]);
break;

case 3:
printf("Enter the Binary number\n");
scanf("%d",&b1);
z=1,o=0,j=0;
while(b1>0)
{
r=b1%2;
if(r>1)
{
printf("Invalid entry");
exit(0);
}
o=o+(r*z);
z=z*2;
b1=b1/10;
}
while(o>0)
{
r=o%8;
y2[j]=r;
j++;
o=o/8;
}
printf("Octal equivalent = ");
for(i=j-1;i>=0;i--)
printf("%d",y2[i]);
break;

case 4:
printf("Enter the Octal number\n");
scanf("%d",&o1);
z=1,b=0,j=0;
while(o1>0)
{
r=o1%10;
if(r>7)
{
printf("Invalid entry");
exit(0);
}
b=b+(r*z);
z=z*8;
o1=o1/10;
}
while(b>0)
{
r=b%2;
y3[j]=r;
j++;
b=b/2;
}
printf("Binary equivalent = ");
for(i=j-1;i>=0;i--)
printf("%d",y3[i]);
break;

case 5:
printf("Enter the Binary number\n");
scanf("%d",&b2);
z=1,h=0,j=0;
while(b2>0)
{
r=b2%2;
if(r>1)
{
printf("Invalid entry");
exit(0);
}
h=h+(r*z);
z=z*2;
b2=b2/10;
}
while(h>0)
{
r=h%16;
y4[j]=r;
j++;
h=h/16;
}
printf("Hexadecimal equivalent = ");
for(i=j-1;i>=0;i--)
{
switch(y4[i])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d",y4[i]);
break;
}
}
break;

case 6:
printf("Enter the Hexadecimal number\n");
scanf("%s",x);
printf("Binary equivalent = ");
for(i=0;x[i]!='\0';i++)
{
a=x[i];
switch(a)
{
case '0':
printf("0000");
break;
case '1':
printf("0001");
break;
case '2':
printf("0010");
break;
case '3':
printf("0011");
break;
case '4':
printf("0100");
break;
case '5':
printf("0101");
break;
case '6':
printf("0110");
break;
case '7':
printf("0111");
break;
case '8':
printf("1000");
break;
case '9':
printf("1001");
break;
case 'A':
printf("1010");
break;
case 'B':
printf("1011");
break;
case 'C':
printf("1100");
break;
case 'D':
printf("1101");
break;
case 'E':
printf("1110");
break;
case 'F':
printf("1111");
break;
default:
printf("Invalid entry");
break;
}
}
break;

case 7:
exit(0);
}
}
}




You Like It!? Then kindly share with your Friends.
Comments
1 Comments

1 comment:

Unknown said...

//ubuntu
/*use void, conio.h, clrscr(); ,getch(); in windows c*/
#include
#include
main()
{
int n,x;
void d2b(int);void b2d();void d2o(int);void o2d();void d2h();void h2d();
printf("\t\tMenu\n1.Decimal to Binary\n2.Binary to Decimal\n3.Decimal to Octal");printf("\n4.Octal to Decimal\n5.Decimal to Hexadecimal\n6.Hexadecimal to Decimal\nEnter your choice:");
scanf("%d",&n);
if((n==1)||(n==3))
{
printf("Enter decimal no:");
scanf("%d",&x);
}
switch(n)
{
case 1:d2b(x);break;
case 2:b2d();break;
case 3:d2o(x);break;
case 4:o2d();break;
case 5:d2h();break;
case 6:h2d();break;
}
}
void d2b(int x)
{
int t,r,a[20],i=0;
t=x;
while(t)
{
a[i]=t%2;
t/=2;
i++;
}i--;
printf("\nBinary is :");
for(;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
void b2d()
{
long int b,t;int i=0,r,s=0;
printf("\nEnter Binary no:");
scanf("%d",&b);
t=b;
while(t)
{
r=t%10;
s=s+(r*pow(2,i));
i++;
t/=10;
}
printf("Decimal of %d is %d",b,s);
}
void d2o(int x)
{
int t,s=0,z=0,a[7],i=0;
t=x;
while(t)
{
a[i]=t%8;
t/=8;
i++;
}i--;
printf("Octal of %d is %d",x,z);
for(;i>=0;i--)
printf("%d",a[i]);
}
void o2d()
{
int o,t,d=0,r,i=0,s=0;
printf("\nEnter the octal no: ");
scanf("%d",&o);
t=o;
while(t)
{
r=t%10;
s=s+(r*pow(8,i));
i++;
t/=10;
}

printf("\nDecimal of %d is %d",o,s);d2b(s);
}
void d2h()
{
int d,t,r,i=0;
char c[10],z;
printf("\nEnter the decimal no: ");
scanf("%d",&d);t=d;
while(t)
{
r=t%16;
if(r==10)
c[i]= 'A';
else if(r==11)
c[i]= 'B';
else if(r==12)
c[i]= 'C';
else if(r==13)
c[i]= 'D';
else if(r==14)
c[i]= 'E';
else if(r==15)
c[i]= 'F';
else
c[i]=r+48;
i++;
t/=16;
}
z=strlen(c);
printf("Hexadecimal of %d is ",d);
for(i--;i>=0;i--)
printf("%c",c[i]);
}
void h2d()
{
int r,l,i=0,n=0;
char a[10];
printf("\nEnter the hexa no: ");
scanf("%s",&a);
l=strlen(a);
while(l)
{
a[i]=toupper(a[i]);
switch(a[i])
{
case 'A':r=10;break;
case 'B':r=11;break;
case 'C':r=12;break;
case 'D':r=13;break;
case 'E':r=14;break;
case 'F':r=15;break;
default:r=a[i]%16;
}n+=r*pow(16,l-1);
i++;l--;
}
printf("\nDecimal is %d",n);
d2b(n);
}



Try this also
from abdullahashir04@gmail.co

Post a Comment