Various Base Conversions,Binary to Decimal, Decimal to Binary, Binary to Hexadecimal,Hexadecimal to Binary, Binary to Octal, Octal to Binary

#include
#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);
}

Related Links :

1 comment:


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!