Thursday 14 June 2012

Conversion of Binary to Hexa Decimal, Decimal, Octal

/*
#include<stdio.h>
#include<math.h>
int basetodec(int n, int b)
{
int s=0,i=0,d;
while(n>0)
{
d=n%10;
if(d>0)
s=s+pow(b,i)*d;
i++;
n/=10;
}
return s;
}
int dectobase(int n,int b)
{
int i=1,s=0,d;
while(n>0)
{
d=n%b;
s=s+i*d;
n/=b;
i*=10;
}
return s;
}
main()
{
char a[15];
int ext,n,b,d,g,sum=0,c,i=0,x,f=0,ex;
a: printf("\n\n\t-------Select your Choice--------\n");
printf("\n\n\t1. Binary Number to Decimal Number");
printf("\n\t2. Binary Numberto Hexa Decimal Number");
printf("\n\t3. Binary Number to Octal Number");
printf("\n\t4. Decimal Number to Binary Number");
printf("\n\t5. Hexa Decimal Number to Binary Number");
printf("\n\t6. Octal Number to Binary Number");
printf("\n\t7. Exit");
printf("\n--------------------------------------------------\n");
b: printf("\nEnter your Choice : ");
scanf("%d",&c);
if(c<7)
printf("Enter the Number : ");
if(c==5)
{
scanf("%s",a);
while((a[i]!='\0')&&(f==0))
{
if((a[i]>=65&&a[i]<=70)||(a[i]>=45&&a[i]<=59))
f=0;
else
{
f=1;
printf("\nINVALID HEXA DECIMAL NUMBER\n");
goto b;
}
x=i;
i++;
}
}
else
{
scanf("%d",&n);
b=n;
if((c==1)||(c==2)||(c==3))
{
while(b>0)
{
d=b%10;
if((d==0)||(d==1))
b/=10;
else
{
b=0,f=1;
printf("\nINVALID BINARY NUMBER\n");
goto b;
}
}
}
else
{
if(c==6)
{
while(b>0)
{
d=b%10;
if(d>7)
{
f=1;
printf("\nINVALID HEXA DECIMAL
NUMBER\n");
goto b;
}
b/=10;
}
}
}
}
if(f==0)
{
switch(c)
{
case 1:
printf("\nThe Decimal Equivalent of the given Binary
Number is : %d",basetodec(n,2));
break;
case 2:
b=basetodec(n,2);
i=0;
while(b>0)
{
d=b%16;
if((d<17)&&(d>=0))
a[i]=d+48;
if((d<17)&&(d>=10))
a[i]=d+55;
b/=16;
i++;

}
x=0;
printf("\nThe Hexa Decimal Equivalent of the given
Binary Number is : ");
while(x<i)
{
printf("%c",a[x]);
x++;
}
break;
case 3:
printf("\nThe Octal Equivalent of the given Binary
Number is : %d",dectobase(basetodec(n,2),8));
break;
case 4:
printf("\nThe Binary Equivalent of the given Decimal
Number is : %d",dectobase(n,2));
break;
case 5:
i=0;
while(x>=0)
{
g=pow(16,i);
if((a[x]<=57)&&(a[x]>=48))
b=a[x]-48;
if((a[x]<71)&&(a[x]>=65))
b=a[x]-55;
sum=b*g+sum;
x--;
i++;
}
printf("\nThe Binary Equivalent of the given Hexa
Decimal Number is : %d",dectobase(sum,2));
break;
case 6:
printf("\nThe Binary Equivalent of the given Octal
Number is : %d",dectobase(basetodec(n,8),2));
break;
case 7:
printf("\n\nDO YOU WANT TO QUIT(Y/N) : \n");
printf("press 1 to continue..........: ");
scanf("%d",&ex);
if(ex==1)
goto a;
else
exit(0);
default:printf("\nYOUR CHOICE IS WRONG\n");
break;
}
printf("\n\nDO YOU WANT TO CONTINUE(Y/N) : \n");
printf(" press 1 to continue........... : ");
scanf("%d",&ext);
if(ext== 1)
goto a;
else
exit(1);

}
}
/*

--------------OUTPUT---------------


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 1
Enter the Number : 323

INVALID BINARY NUMBER

-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------


Enter your Choice : 1
Enter the Number : 1111

The Decimal Equivalent of the given Binary Number is : 15

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 2
Enter the Number : 1111

The Hexa Decimal Equivalent of the given Binary Number is : F

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 3
Enter the Number : 1111

The Octal Equivalent of the given Binary Number is : 17

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 4
Enter the Number : 10

The Binary Equivalent of the given Decimal Number is : 1010

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 5
Enter the Number : 15

The Binary Equivalent of the given Hexa Decimal Number is : 10101

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1









-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 6
Enter the Number : 10

The Binary Equivalent of the given Octal Number is : 1000

DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 1


-------Select your Choice--------


1. Binary Number to Decimal Number
2. Binary Numberto Hexa Decimal Number
3. Binary Number to Octal Number
4. Decimal Number to Binary Number
5. Hexa Decimal Number to Binary Number
6. Octal Number to Binary Number
7. Exit
--------------------------------------------------

Enter your Choice : 9

YOUR CHOICE IS WRONG


DO YOU WANT TO CONTINUE(Y/N) :
press 1 to continue........... : 0


_______________________________________________________________________


*/



No comments:

Post a Comment