C program to convert decimal number into hexadecimal

C program to convert decimal number into hexadecimal:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
#include<math.h>

//Function for finding hexadecimal number
void dec_to_hex(long int num)
{
  long int rem[50],i=0,len=0;

  while(num>0)
   {
      rem[i]=num%16;
      num=num/16;
      i++;
      len++;
   }

  //Printing Hexadecimal Numbers
  printf("Hexadecimal number : ");
  for(i=len-1;i>=0;i--)
  {
    switch(rem[i])
    {
      case 10:
          printf("A");
          break;
      case 11:
          printf("B");
          break;
      case 12:
          printf("C");
          break;
      case 13:
          printf("D");
          break;
      case 14:
          printf("E");
          break;
      case 15:
          printf("F");
          break;
      default :
         printf("%ld",rem[i]);
    }
  }
}

//Main Function
main()
{
long int num;
 printf("Enter the decimal number : ");
 scanf("%ld",&num);

    dec_to_hex(num);
}

OUTPUT:

Enter the decimal number : 15
Hexadecimal number : F

Enter the decimal number : 156
Hexadecimal number : 9C

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.