SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

C Program on Decimal to Hexadecimal Number Conversion

11 May 2015

Write a Program in C to input a number in Decimal number system and convert it into its equivalent number in the Hexadecimal number system.

Question:

Write a C program to input a number in the decimal number system and convert it into its equivalent hexadecimal number.

Hexadecimal Number System: The hexadecimal number system has sixteen symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. The decimal values 10, 11, 12, 13, 14 and 15 are represented by A, B, C, D, E and F respectively.

Example: The decimal number 47 is written as 2F in hexadecimal, because 47 divided by 16 gives quotient 2 and remainder 15, and 15 is represented by F.

decimal to hexadecimal conversion

Algorithm:

Step 1: Start.

Step 2: Declare integer variables deci, i, j and rem.

Step 3: Declare a character array hex to store hexadecimal digits and initialize another array dig with characters from 0 to F.

Step 4: Input the decimal number in deci.

Step 5: If deci is 0, print 0 as the hexadecimal number and stop.

Step 6: Repeat while deci > 0.

Step 7: Find rem = deci % 16.

Step 8: Store dig[rem] in hex[i] and increase i by 1.

Step 9: Divide deci by 16 and store the quotient back in deci.

Step 10: After the loop ends, print the characters of hex in reverse order from i - 1 to 0.

Step 11: Stop.

Explanation:

Decimal to hexadecimal conversion is based on repeated division by 16. Since hexadecimal has base 16, each division gives one hexadecimal digit as a remainder. If the remainder is between 0 and 9, the same digit is used. If the remainder is 10, 11, 12, 13, 14 or 15, the corresponding hexadecimal digit is A, B, C, D, E or F. To make this conversion simple, the program stores all sixteen possible hexadecimal symbols in the character array dig.

The program first reads the decimal number. A special check is made for 0 because the loop while(deci > 0) will not run when the input is 0. In that case, the hexadecimal form is also 0, so the program prints the answer directly. For any positive number, the program repeatedly finds the remainder after division by 16. That remainder is used as an index in the dig array, and the selected hexadecimal character is stored in the hex array.

The remainders are produced from right to left. For example, decimal 47 first gives remainder 15, which is F, and then gives remainder 2. The correct hexadecimal result is 2F, not F2. Therefore, after collecting all digits, the program prints the hex array backwards. This avoids the non-standard strrev() function and keeps the program portable across standard C compilers.

C Program:

C
/*
 * C Program to convert a Decimal number into a Hexadecimal number
 * @author : www.guideforschool.com
 * @Program Type : C Program
 */

#include <stdio.h>

int main(void)
{
    int deci, i = 0, j, rem;
    char hex[100]; // Array for storing hexadecimal digits.
    char dig[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    printf("\nEnter a decimal number : ");
    scanf("%d", &deci);

    if(deci == 0)
    {
        printf("\nEquivalent Hexadecimal number = 0");
        return 0;
    }

    while(deci > 0)
    {
        rem = deci % 16;
        hex[i++] = dig[rem]; // Store digit according to the remainder.
        deci = deci / 16;
    }

    printf("\nEquivalent Hexadecimal number = ");

    // Print the stored digits in reverse order.
    for(j = i - 1; j >= 0; j--)
    {
        printf("%c", hex[j]);
    }

    return 0;
}

Output:

Plain
Enter a decimal number : 47
Equivalent Hexadecimal number = 2F

Enter a decimal number : 1243
Equivalent Hexadecimal number = 4DB

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →