> Number System Comments Feed" href="https://www.c4learn.com/program-for-decimal-to-hexadecimal.html/feed"/>

Program for Decimal to Hexadecimal Conversion in C >> Number System

February 6, 2025 No Comments » Hits : 878





Program to Convert Decimal number into Hexadecimal : Number System : Download


Logic of This Program :

  1. Accept Number from User
  2. As we are dividing number by 16 , we have only remainders 0 to 15 etc
  3. Take an array of remainder i. rem[20];
  4. Let ,Initial elements in rem[] be Zero
  5. In each iteration Store Remainder in the Array & Divide original number by 16
  6. Also Count the number of remainders stored
  7. To Convert into Hexadecimal Just Reverse Remainder array
  8. If the Remainder Array Element is greater than 10 than Use Following Table
RemainderDisplay this
10A
11B
12C
13D
14E
15F

Program :

#include<stdio.h>
#include<conio.h>
#include<math.h>
void dec_hex(long int num)   // Function Definition
{
long int rem[50],i=0,length=0;
while(num>0)
   {
      rem[i]=num%16;
      num=num/16;
      i++;
      length++;
   }
printf("Hexadecimal number : ");
for(i=length-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]);
    }
  }
}
//================================================
void main()
{
long int num;
clrscr();
 printf("Enter the decimal number : ");
 scanf("%ld",&num);
    dec_hex(num);   // Calling function
 getch();
}

Output :

Enter the decimal number : 87274
Hexadecimal number : 154EA

Download Code

Incoming search terms: