You are on page 1of 9

#include<stdio.

h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
long int bin_to_dec(char *);
int *bin_to_oct(char *);
long int oct_to_dec(long int);
int *oct_to_bin(long int);
void oct_to_hex(long int);
long int hex_to_dec(char *);
int *hex_to_bin(char *);
int *hex_to_oct(char *);
void bin_to_hex(char *);
float binfr_to_decfr(char *);
void binfr_to_octfr(char *);
void binfr_to_hexfr(char *);
void binry_to_other();
void octl_to_other();
int *dec_to_bin(long int);
int *dec_to_oct(long int);
void dec_to_hex(long int);
void dec_to_binfr(long double);
void dec_to_octfr(long double);
void dec_to_hexfr(long double);
int i,*p,k,local,count;
int z[100];
double v=0;
float f;
long int l=0,d,s;
char *ptr,*ptr1,*ptr2;
long int b,temp;
main()
{
char reply;
int choice;
float a;
long double c;
int *rec;
char *str=(char *)malloc(sizeof(char)*100);
do
{
printf("1) Decimal to Binary \n\t\t\t4) Binary to Decimal \n");
printf("2) Decimal to Octal \n\t\t\t5) Binary to Octal
\n");
printf("3) Decimal to hexadecimal
\n\t\t\t6) Binary to hexadecima
l
\n\n \n");
printf("7) Octal to Decimal \n\t\t\t10) Hexadecimal to Decimal \n");
printf("8) Octal to Binary \n\t\t\t11) Hexadecimal to Binary\n");
printf("9) Octal to Hexadecimal
\n\t\t\t12) Hexadecimal to
Octal\n");
printf("Press 0 to Quit\n");
z:
printf("\nEneter choice: ");
scanf("%d",&choice);
do
{
switch(choice)

{
case 1:
do
{
printf("\nEnter a Decimal number:");
scanf("%f",&a);
b=(int)a;
c=a-b;
if(a<0)
{
printf("Not Possible\n");
printf("Try Again\n");
}
}while(a<0);
printf("\nBinary =");
rec=dec_to_bin(b);
for(i=i-1;i>=0;i--)
printf("%d",*(rec+i));
break;
case 2:
do
{
printf("\nEnter a Decimal number:");
scanf("%f",&a);
b=(int)a;
c=a-b;
if(a<0)
{
printf("Error.\n");
}
}while(a<0);
printf("\nOctal=");
rec=dec_to_oct(b);
for(i=i-1;i>=0;i--)
printf("%d",*(rec+i));

break;
case 3:
do
{
printf("\nEnter a Decimal number:");
scanf("%f",&a);
b=(int)a;
c=a-b;
if(a<0)
{
printf("Error ");
}
}while(a<0);
printf("\nHexadecimal=");
dec_to_hex(b);

break;
case 4:
binry_to_other();
if(*(ptr2)!='\0')
printf("\nDecimal%f\n\n",bin_to_dec(ptr1)+binfr_to_decfr(ptr2))
;
else
printf("\nDecimal%ld\n\n",bin_to_dec(ptr1));
break;
case 5:
binry_to_other();
rec=bin_to_oct(ptr1);
printf("Octal=");
for(i=i-1;i>=0;i--)
printf("%d",*(rec+i));
break;
case 6:
binry_to_other();
printf("\nHexadecimal=");
for(i=i-1;i>=0;i--)
bin_to_hex(ptr1);
break;
case 7:
octl_to_other();
s=oct_to_dec(b);
printf("\nDecimal=%ld\n\n",s);
break;
case 8:
octl_to_other();
rec=oct_to_bin(b);
printf("\nBinary=");
for(i=i-1;i>=0;i--)
printf("%d",*(rec+i));
printf("\n\n");
break;
case 9:
octl_to_other();
printf("\nHexadecimal=");
oct_to_hex(b);
break;
case 10:
printf("Enter a hexa number:");
scanf("%s",str);
for(l=0;*(str+l)!=NULL;l++);
printf("\nDecimal=%ld\n\n",hex_to_dec(str));
break;

case 11:
printf("Enter a hexa number:");
scanf("%s",str);
for(l=0;*(str+l)!=NULL;l++);
rec=hex_to_bin(str);
printf("\nBinary=");
for(i=i-1;i>=0;i--)
printf("%d",*(rec+i));
printf("\n\n");
break;
case 12:
printf("Enter a hexa number:");
scanf("%s",str);
for(l=0;*(str+l)!=NULL;l++);
rec=hex_to_oct(str);
printf("\nOctal=");
for(i=i-1;i>=0;i--)
printf("%d",*(rec+i));
printf("\n\n");
break;
case 0:
exit(0);
default:
printf("\n\n**\tINVALID \t**\n\n");
getchar();goto z;
}
b:
getchar();
printf("\nPress z to continue.Press x to go to main menu.\n");
scanf("%c",&reply);
if(reply!='x'&&reply!='z')
{
printf("\n\nINVALID CHOICE\n\n");
goto b;
}
}while(reply!='x');
}while(reply!='z');

getch();
}
int * dec_to_bin(long int a)
{
i=0,v=0;
if(a==0)
{
z[0]=0;
i++;
}
while(a!=0)

{
z[i]=a%2;
a=a/2;
i++;
}
p=&z[0];
return (p);
}

int *dec_to_oct(long int a)


{
i=0,v=0;
if(a==0)
{
z[0]=0;
i++;
}
while(a!=0)
{
z[i]=a%8;
a=a/8;
i++;
}
p=&z[0];
return (p);
}

void dec_to_hex(long int a)


{
char hexa[100];
i=0;
int q;
if(a==0)
printf("%ld\n",a);
while(a>0)
{
if((a%16)>=10)
{
hexa[i]=65+(a%16)-10;
i++;
a=a/16;
}
else
{
hexa[i]=48+a%16;
i++;
a=a/16;
}
}
hexa[i]='\0';
for(i=i-1;i>=0;i--)
printf("%c\n",hexa[i]);
}

void dec_to_binfr(long double c)


{
int z;
double x;
if(c!=0)
printf(".");
while(c!=0)
{
x=c*2;
z=(int)x;
printf("%ld",z);
c=x-z;
}
printf("\n\n");
}
long int bin_to_dec(char *b)
{
s=0;
int j=0,m,k;
for(i=l-1;i>=0;i--)
{
k=*(b+i)-48;
m=(int)pow(2,j);
s=s+(m*k) ;
j++;
}
return(s);
}

int *bin_to_oct(char *b)


{
s=bin_to_dec(b);
return(dec_to_oct(s));
}

void bin_to_hex(char *b)


{
s=bin_to_dec(b);
dec_to_hex(s);
}

float binfr_to_decfr(char *p)


{
int j;
float f=0,m;
i=0;
for(j=-1;j>=local-count;j--)
{
char x=*(p+i)-48;
float v=(float)x;
m=pow(2,j);

f+=v*m;
i++;
}
return(f);
}
long int oct_to_dec(long int b)
{
int s=0;
for(i=0;b>0;i++)
{
k=b%10;
b=b/10;
s+=k*(int)pow(8,i);
}
return(s);
}
int *oct_to_bin(long int b)
{
s=oct_to_dec(b);
return (dec_to_bin(s));
}
void oct_to_hex(long int b)
{
s=oct_to_dec(b);
dec_to_hex(s);
}

long int hex_to_dec(char *c)


{
v:
i=0;
d=0;
if(*(c)==45)
{printf("Error\n\n");
goto w;}
while(*(c+i)!=NULL)
{
if((*(c+i))>=65&&*(c+i)<=70)
{
d+=(*(c+i)-55)*pow(16,l-i-1);
}
else if(*(c+i)<=57&&*(c+i)>=48)
{
d+=(*(c+i)-48)*pow(16,l-i-1);
}else
{
printf("\nINVALID\n");
w:
printf("Enter a hexa number:");
scanf("%s",c);
for(l=0;*(c+l)!=NULL;l++);
goto v;
}
i++;

}
return (d);
}

int * hex_to_bin(char *c)


{
d=hex_to_dec(c);
return(dec_to_bin(d));
}
int *hex_to_oct(char *c)
{
d=hex_to_dec(c);
return(dec_to_oct(d));
}
void binry_to_other()
{
int *rec,temp=0;
int tr=0;
ptr=(char *)malloc(sizeof(char)*100);
ptr1=(char *)malloc(sizeof(char)*100);
ptr2=(char *)malloc(sizeof(char)*100);
do
{
temp=0;
printf("\nEnter a binary number:");
scanf("%s",ptr);
for(count=0;*(ptr+count)!=NULL;count++);
for(i=0;*(ptr+i)!=NULL;i++)
{
if(*(ptr+i)!=48&&*(ptr+i)!=49&&*(ptr+i)!='.')
{
printf("INVALID \n\n\n");
temp=1;
break;
}
}
}while(temp);
i=0;
while(*(ptr+i)!='\0')
{
if(*(ptr+i)!='.')
{
*(ptr1+i)=*(ptr+i);
}
else
{
tr=1;
break;
}
i++;
}
*(ptr1+i)='\0';
i++;

local=i;
if(tr)
{
int k=0;
while(*(ptr+i)!=NULL)
{
*(ptr2+k)=*(ptr+i);
i++;
k++;
}
*(ptr2+k)='\0';
for(l=0;*(ptr+l)!='.';l++);
}
else
{
*(ptr2)='\0';
for(l=0;*(ptr+l)!='\0';l++);
}
}
void octl_to_other()
{
int tp;
do
{ p: tp=0;
printf("\nEnter an Octal Number:");
scanf("%ld",&b);
temp=b;
if(temp<0)
{
printf("Error\n\n");
goto p;
}
for(i=0;temp>0;i++)
{
if(temp%10>7&&temp%10<=9)
{
printf("\nINVALID\n");
tp=1;
break;
}
temp=temp/10;
}
}while(tp);
}

You might also like