#include <stdio.h>
#include <stdlib.h>
void find_binary (int num, char str[]);
int find_int (char str[100]);
int main()
{
int i = 0, num, choice, decimal;
char str[100] = {'\0'};
do
{
printf("\nAvailable choices:\n 1. Display a decimal integer in binary format\n 2. Convert from binary format to a decimal integer\n 3. Quit\nEnter the number of your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nEnter an integer: ");
scanf("%d", &num);
printf("\nThe decimal number %d has the binary format ", num);
find_binary(num, str);
break;
case 2:
printf("\nEnter a binary number (as 0s and 1s): ");
scanf("&c", &str);
printf("\nThe binary number %s has the decimal value ", str);
printf("%d", find_int(str));
break;
}
}while(choice == 1 || choice == 2);
return 0;
}
int find_int(char str[100])
{
int decimal = 0, i = 0, remainder, j, k;
j = atoi(str);
while (j != 0)
{
remainder = j % 10;
decimal = decimal + remainder * k;
k = k * 2;
j = j / 10;
}return decimal;
}
void find_binary (int num, char str[100])
{
int i = 0, length = 0;
while(num > 0)
{
str[i] = num % 2;
num = num / 2;
i++;
length++;
}
for(i = length - 1; i >= 0; i--)
printf("%d", str[i]);
}