#include
#include
#include
#define SIZE 20
int binary_to_decimal(int);
void decimal_to_binary(int);
void binary_to_octal(int);
void octal_to_binary(int);
void main()
{
int ch=0,num=0;
clrscr();
do
{
printf("\n--------------Menu------------------");
printf("\n1_Binary to octal");
printf("\n2_octal to binary");
printf("\n3_Exit from program");
printf("\n\nEnter your choice<1-3>:");
scanf("%d",&ch);
switch(ch)
{
case 1: // Binary to octal
printf("\nEnter the binary number:");
scanf("%d",&num);
printf("\nThe octal equivalent of %d is ",num);
binary_to_octal(num);
break;
case 2: // Octal to Binary
printf("\n\nEnter the octal number:");
scanf("%d",&num);
printf("\n\nThe Binary equivalent of %d is ",num);
octal_to_binary(num);
break;
case 3: // Exit
exit(0);
break;
default:
printf("\nWrong choice!!!!!!");
break;
}
}
while(ch!=3);
}
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;
}
void decimal_to_binary(int num)
{
int array[SIZE],i=0;
while(num!=0)
{
array[i++]=num%2;
num/=2;
}
for(--i;i>=0;i--)
printf("%d",array[i]);
}
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;
}
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);
}
No comments:
Post a Comment