Here we written code to Convert Decimal to Binary or Hexa decimal by user choice.
For improvement of your coding skills we give some tasks along this. If you solve this tasks and send to our email (onlineexamshubteam@gmail.com) with your details. we will display your details(like Name, City, college, photo) in our site.
Task: Write a C program to Convert Hexa decimal value to Binary and vice versa.
Write a C Program to convert decimal to binary and hex (using switch call function the function)
#include <stdio.h>
void dectobin(long int);
void dectohex(long int);
void main()
{
int c;
long num;
printf(“Enter a decimal integer: “);
scanf(“%ld”, &num);
printf(“1. Decimal TO Binary\n2. Decimal to HexaDecimal\n”);
printf(“Choose conversion type : “);
scanf(“%d”,&c);
printf(“Input number is : %ld”,num);
switch (c)
{
case 1:
dectobin(num);
break;
case 2:
dectohex(num);
break;
default:
printf(“\nWronge choice\n”);
}
}
void dectobin(long int dec_num)
{
int i=0,remainder;
char bi[1024];
while (dec_num > 0)
{
remainder = dec_num % 2;
bi[i++]=remainder+’0′;
dec_num = dec_num / 2;
}
printf(“Its binary equivalent is : “);
for(–i;i>=0;i–)
printf(“%c”,bi[i]);
}
void dectohex(long int dec_num)
{
int i=0,remainder;
char he[1024];
while(dec_num>0)
{
remainder = dec_num % 16;//To convert integer into character
if( remainder < 10)
remainder =remainder + 48;
else
remainder = remainder + 55;
he[i++]= remainder;
dec_num = dec_num / 16;
}
printf(“\nIts hexa equivalent is : “);
for(–i;i>=0;i–)
printf(“%c”,he[i]);
}
Output:
Enter a decimal integer: 16
1. Decimal TO Binary
2. Decimal to HexaDecimal
Choose conversion type : 2
Input number is : 16
Its hexa equivalent is : 10
Task Achievers