C Program to Convert Decimal Number to Hexadecimal Number

Decimal number system is a base 10 number system using digits 0 and 9 whereas Hexadecimal number system is base 16 number system and using digits from 0 to 9 and A to F. Given an decimal number as input from user convert it to hexadecimal number.
For Example:
1234 in Decimal is equivalent to 4D2 in Hexadecimal number system.

Required Knowledge


C program to convert a decimal number to hexadecimal number

#include <stdio.h>  
#include <string.h>  
  
#define BASE_16 16

int main() {  
    char hexDigits[] = "0123456789ABCDEF"; 
  
    long decimalNumber;  
    char hexadecimalNumber[40];  
    int index, remaindar;  
      
    /* 
     * Take a Decimal Number as input form user 
     */  
    printf("Enter a Decimal Number\n");  
    scanf("%ld", &decimalNumber);   
  
    index = 0;
     
    /* Convert Decimal Number to Hexadecimal Numbers */  
    while(decimalNumber != 0) {  
        remaindar = decimalNumber % BASE_16;  
        hexadecimalNumber[index] = hexDigits[remaindar];  
        decimalNumber /= BASE_16;  
        index++;  
    }  
    hexadecimalNumber[index] = '\0';  
  
    strrev(hexadecimalNumber);  
  
    printf("Hexadecimal Number : %s", 
        hexadecimalNumber);  
  
    return 0;  
} 
Output
Enter a Decimal Number
1234
Hexadecimal Number : 4D2
Enter a Decimal Number
1000
Hexadecimal Number : 3E8

Related Topics
C program to convert binary numbers to octal number using function
C program to convert decimal numbers to binary numbers
C program to convert binary number to decimal number system
C program to convert kilometer to miles
C program to convert decimal numbers to binary numbers
C program to convert decimal number to octal number
C program to convert hexadecimal number to decimal number system
C program to convert temperature from celsius to fahrenheit
C program to make a simple calculator using switch statement
List of all C programs