#include
#define SIZE 20
int binary_to_decimal(int);
int decimal_to_binary(int);
void binary_to_hexa(int);
int hexa_to_binary(char []);
void binary_to_octal(int);
void octal_to_binary(int);
int main()
{
int choice=0,number=0;
char hexa[10];
do
{
printf("\n\n\tMenu\n\t
1.Binary to Decimal
\n\t2.Decimal to Binary
\n\t3.Binary to Hexadecimal
\n\t4.Hexadecimal to Binary
\n\t5.Binary to Octal
\n\t6.Octal to Binary
\n\t7.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: // Binary to Decimal
printf("Enter Binary Number :\t");
scanf("%d",&number);
printf("Decimal Number :\t%d",binary_to_decimal(number));
break;
case 2: // Decimal to Binary
printf("Enter Decimal Number :\t");
scanf("%d",&number);
decimal_to_binary(number);
break;
case 3: // Binary to Hexadecimal
printf("Enter Binary Number :\t");
scanf("%d",&number);
binary_to_hexa(number);
break;
case 4: // Hexadecimal to Binary
printf("Enter Hexadecimal Number :\t");
scanf("%s",&hexa);
hexa_to_binary(hexa);
break;
case 5: // Binary to Octal
printf("Enter Binary Number :\t");
scanf("%d",&number);
binary_to_octal(number);
break;
case 6: // Octal to Binary
printf("Enter Octal Number :\t");
scanf("%d",&number);
octal_to_binary(number);
case 7: // Exit
break;
default:break;
}
}
while(choice!=7);
}
int binary_to_decimal(int num)
{
int i=0,sum=0;
while(num!=0)
{
sum=sum+(num%10)*pow(2,i++);
num/=10;
}
return sum;
}
int decimal_to_binary(int num)
{
int array[SIZE],i=0;
while(num!=0)
{
array[i++]=num%2;
num/=2;
}
printf("Binary Number :\t");
for(--i;i>=0;i--)
printf("%d",array[i]);
}
void binary_to_hexa(int num)
{
int array[SIZE],i=0;
num=binary_to_decimal(num);
if(num==0)
{
array[i++]=0;
}
while(num!=0)
{
array[i++]=num%16;
num/=16;
}
printf("\nHexadecimal No :\t");
for(--i;i>=0;i--)
{
if(array[i]>=10&&array[i]<=15)
printf("%c",array[i]+55);
else
printf("%d",array[i]);
}
}
int hexa_to_binary(char s[])
{
int a[SIZE],i,dn=0,j=0;
for(i=0;s[i]!=NULL;i++)
{
if(s[i]<=57&&s[i]>=48)
a[i]=s[i]-48;
if(s[i]>=65&&s[i]<=70)
a[i]=s[i]-55;
else
{
printf("Wrong Input");
return;
}
}
for(;--i>=0;)
{
dn+=a[i]*pow(16,j++);
}
decimal_to_binary(dn);
}
void binary_to_octal(int num)
{
int array[SIZE],i=0;
num=binary_to_decimal(num);
if(num==0)
{
array[i++]=0;
}
while(num!=0)
{
array[i++]=num%8;
num/=8;
}
printf("Octal Number :\t");
for(--i;i>=0;i--)
printf("%d",array[i]);
}
void octal_to_binary(int num)
{
int i=0,sum=0;
while(num!=0)
{
sum=sum+(num%10)*pow(8,i++);
num/=10;
}
decimal_to_binary(sum);
}
usng pow() will be better
ReplyDelete