C Program to Implement Number System Conversion
This is the C program to implement number system conversion,it converts 1.Decimal to Hexadecimal 2.Hexadecimal to Decimal
#include<stdio.h>
#include<math.h>
#include<string.h>
main()
{
int ch;
printf("Choose any option 1.Decimal to Hexadecimal 2.Hexadecimal to Decimaln");
scanf("%d",&ch);
switch(ch)
{
case 1:dechex();
break;
case 2:hexdec();
break;
default:printf("nInvalid Option");
}
}
int dechex()
{
int dec,num;
printf("Enter a Decimal Numbern");
scanf("%d",&dec);
num=dec;
int digit;
char res[100];
int k=0;
while (num!=0)
{
digit=num%16;
res[k]=jp(digit);
k++;
num=num/16;
}
int i;
i=strlen(res);
int j;
for(j=i-1;j>=0;j--)
{
printf("%c",res[j]);
}
printf("n");
}
int jp(int digit)
{
return "0123456789ABCDEF"[digit];
}
int hexdec()
{
char arr[100];
int num[100];
printf("Enter a Hexadecimal Numbern");
scanf("%s",arr);
int a=0,ctr=0,nDig;
a=strlen(arr);
nDig=a;
int b;
for(b=nDig-1;b>=0;b--)
{
if (arr[b]=='A')
{
num[b]=10;
}
else if(arr[b]=='B')
{
num[b]=11;
}
else if(arr[b]=='C')
{
num[b]=12;
}
else if(arr[b]=='D')
{
num[b]=13;
}
else if(arr[b]=='E')
{
num[b]=14;
}
else if(arr[b]=='F')
{
num[b]=15;
}
else if(arr[b]=='0')
{
num[b]=0;
}
else if(arr[b]=='1')
{
num[b]=1;
}
else if(arr[b]=='2')
{
num[b]=2;
}
else if(arr[b]=='3')
{
num[b]=3;
}
else if(arr[b]=='4')
{
num[b]=4;
}
else if(arr[b]=='5')
{
num[b]=5;
}
else if(arr[b]=='6')
{
num[b]=6;
}
else if(arr[b]=='7')
{
num[b]=7;
}
else if(arr[b]=='8')
{
num[b]=8;
}
else if(arr[b]=='9')
{
num[b]=9;
}
}
int sum=0;
int e,org[100],z=0;
for(e=nDig-1;e>=0;e--)
{
org[z]=num[e];
z++;
}
for(e=0;e<nDig;e++)
{
sum+=org[e]*pow(16,e);
}
printf("nThe Decimal Number is %d n",sum);
}
Recent Comments