Decimal to hexadecimal conversion C Program

Decimal number is a base 10 number and hexadecimal number is base 16 number. In decimal number number there are 0 to 9 digits and in hexadecimal number 0 to 9 digits plus A, B, C, D, E ,F characters. Number 1324 is a decimal number and number 1A2 is hexadecimal number.


/* C program to convert number from decimal to hexadecimal */
#include<stdio.h>
int main()
{
 int decimal, i=0,j=0,num,size;
 char hex[100];
 printf("Enter decimal number: ");
 scanf("%d",&decimal);
 
 num=decimal;
 while(num!=0)
 {
  int rem=num%16;
  switch(rem)
  {
  case 10:
   hex[i]='A';
   break;
  case 11:
   hex[i]='B';
   break;
  case 12:
   hex[i]='C';
   break;
  case 13:
   hex[i]='D';
   break;
  case 14:
   hex[i]='E';
   break;
  case 15:
   hex[i]='F';
   break;
  default:
   hex[i]=rem+'0';
   break;
 
  }
  num=num/16;
  i++;
 }
 hex[i]='\0';
 i--;
 for(j=0;j<i;j++,i--)
 {
  char temp=hex[i];
  hex[i]=hex[j];
  hex[j]=temp;
 }
 printf("%d in hexadecimal = %s",decimal,hex);
 
 return 0;
}

Popular posts from this blog