Â
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
long long int binary;
int octal;
int binaryToOctal()
{
int oct = 0, dec = 0, i = 0;
printf("Enter a binary number: ");
scanf("%lld", &binary);
// converting binary to decimal
while (binary != 0)
{
dec += (binary % 10) * pow(2, i);
++i;
binary /= 10;
}
i = 1;
// converting to decimal to octal
while (dec != 0)
{
oct += (dec % 8) * i;
dec /= 8;
i *= 10;
}
return oct;
}
long long octalToBinary()
{
int dec = 0, i = 0;
long long bin = 0;
printf("Enter an octal number: ");
scanf("%d", &octal);
// converting octal to decimal
while (octal != 0)
{
dec += (octal % 10) * pow(8, i);
++i;
octal /= 10;
}
i = 1;
// converting decimal to binary
while (dec != 0)
{
bin += (dec % 2) * i;
dec /= 2;
i *= 10;
}
return bin;
}
int main()
{
int operation;
printf("-----Select the Operation------\n");
printf("1. Binary to Octal\n2. Octal to Binary\n3. Exit\n");
printf("Enter the corresponding number of Operation: ");
scanf("%d", &operation);
switch (operation)
{
case 1:
printf("%d in octal", binaryToOctal());
break;
case 2:
printf("%lld in binary", octalToBinary());
break;
case 3:
exit(0);
default:
printf("Wrong input");
}
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP