C Program to Convert Decimal to Hexadecimal

In this tutorial, we will learn how to create a program in C that converts any given decimal number (provided by the user at run-time) into its equivalent hexadecimal value. We have also created a user-defined function that does the same job of converting a decimal number to a hexadecimal number.

Before going through the program, if you are not aware of

Then refer to "Decimal to Hexadecimal Conversion: A Step-by Process." Let's move on to the program.

Decimal to hexadecimal in C

To convert a decimal number to a hexadecimal number in C programming, you have to ask the user to enter the decimal number as input and then convert it into its equivalent hexadecimal value, as shown in the program given below. The question is, "Write a program in C that converts decimal to hexadecimal." Here is its answer.

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, rem, i=0;
    char hexnum[50];
    printf("Enter any decimal number: ");
    scanf("%d", &decnum);
    while(decnum!=0)
    {
        rem = decnum%16;
        if(rem<10)
            rem = rem+48;
        else
            rem = rem+55;
        hexnum[i] = rem;
        i++;
        decnum = decnum/16;
    }
    printf("\nEquivalent Value in Hexadecimal = ");
    for(i=i-1; i>=0; i--)
        printf("%c", hexnum[i]);
    getch();
    return 0;
}

Because the above program was built and run using the Code::Blocks IDE, you will receive the following output after a successful build and run. This is the first snapshot of the sample run:

c decimal to hexadecimal

Now supply any decimal number as input, say 172, and press the ENTER key to see the equivalent hexadecimal value as output. Here is the second snapshot of the sample run:

c program convert decimal to hexadecimal

Here is another sample run. This is the final snapshot of the sample run:

decimal to hexadecimal in c

Program Explained

Decimal to Hexadecimal in C without the Modulus Operator

Now let's create the same program but without using any modulus operators. That is, we have to find out the remainder without using the modulus operator. Therefore, to find the remainder, we have divided the number present in decnum by 16 and stored its quotient value inside a variable, say temp, then again multiplied the quotient value with 16 and stored the multiplication result in a variable, say chck. Subtract chck from decnum (decnum - chck) and assign the result to the rem variable, which will store the current remainder.

For example, if the user enters 172 as input, decnum/16 or 10 is initialized to temp on the first run. Then, to check, temp*16, 10*16, or 160 is initialized to chck. Now decnum-chck, 172-160, or 12 is initialized to rem, which is this time the remainder. In this way, we have replaced the modulus operator with three lines of code, as shown in the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int decnum, hexnum[50], temp, chck, i=0, rem;
    printf("Enter any Decimal number: ");
    scanf("%d", &decnum);
    while(decnum!=0)
    {
        temp = decnum/16;
        chck = temp*16;
        rem = decnum - chck;
        if(rem<10)
            rem = rem+48;
        else
            rem = rem+55;
        hexnum[i] = rem;
        i++;
        decnum = temp;
    }
    printf("\nEquivalent Hexadecimal Value = ");
    for(i=i-1; i>=0; i--)
        printf("%c", hexnum[i]);
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c decimal to hexadecimal without modulous

Decimal to Hexadecimal in C using a User-Defined Function

Now let's create a user-defined function named DecToHex() that takes one argument (the decimal number entered by the user). Here we have declared the variable i  and the array hex[] outside both the functions main() and DecToHex() to make them known for both functions. Here, the variable i is declared as a static variable because a static variable remembers its previous value. One last thing is that we have not initialized the static variable i with 0 (which is required), because by default, a static variable holds 0 as its initial value after being declared. The rest of the thing is similar and easy to understand.

#include<stdio.h>
#include<conio.h>
void DecToHex(int dec);
static int i;
char hex[50];
int main()
{
    int decnum;
    printf("Enter any decimal number: ");
    scanf("%d", &decnum);
    DecToHex(decnum);
    printf("\nEquivalent Value in Hexadecimal = ");
    for(i=i-1; i>=0; i--)
        printf("%c", hex[i]);
    getch();
    return 0;
}
void DecToHex(int dec)
{
    int rem;
    while(dec!=0)
    {
        rem = dec%16;
        if(rem<10)
            rem = rem+48;
        else
            rem = rem+55;
        hex[i] = rem;
        i++;
        dec = dec/16;
    }
}

Below is the final snapshot of the sample run of the above program:

c decimal to hexadecimal using function

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!