This C program converts a decimal number into its hexadecimal representation.
Hexadecimal (or hex) is a base-16 numbering system that uses 16 unique digits to represent values from 0 to 15. The digits 0-9 represent their decimal counterparts, and the letters A-F (or a-f) represent the values 10-15.
Problem Statement
Write a C program that converts a decimal number to its hexadecimal representation.
The program should perform the following steps:
- Prompt the user to enter a decimal number.
- Read the decimal number input from the user.
- Implement a function,
decimalToHexadecimal, which takes the decimal number as input and converts it to its hexadecimal equivalent. - Within the
decimalToHexadecimalfunction, perform the conversion by repeatedly dividing the decimal number by 16 and obtaining the remainder. Map each remainder to its corresponding hexadecimal digit. - Store the hexadecimal digits in an array.
- Print the resulting hexadecimal representation by iterating through the array in reverse order.
- Finally, in the
mainfunction, call thedecimalToHexadecimalfunction with the user-provided decimal number and display the hexadecimal representation.
Note:
- The program should handle decimal numbers within the range of integers supported by the C language.
- The program should handle the conversion for positive decimal numbers only.
You can use the provided C program template in the previous response as a starting point to solve the problem.
C program to Convert Decimal to Hexadecimal
#include <stdio.h>
void decimalToHexadecimal(int decimalNumber) {
int quotient, remainder;
char hexadecimalNumber[100];
int i = 0;
quotient = decimalNumber;
while (quotient != 0) {
remainder = quotient % 16;
if (remainder < 10)
hexadecimalNumber[i++] = remainder + 48;
else
hexadecimalNumber[i++] = remainder + 55;
quotient = quotient / 16;
}
printf("Hexadecimal representation: ");
for (int j = i - 1; j >= 0; j--)
printf("%c", hexadecimalNumber[j]);
printf("\n");
}
int main() {
int decimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);
decimalToHexadecimal(decimalNumber);
return 0;
}
How it Works
- The program begins by including the necessary header file,
<stdio.h>, to enable input and output operations. - The
decimalToHexadecimalfunction is defined to convert the decimal number to its hexadecimal representation. It takes an integer parameter,decimalNumber, which is the decimal value to be converted. - Inside the
decimalToHexadecimalfunction:- Variables are declared:
quotientto store the quotient obtained during the division process.remainderto store the remainder obtained during each division.hexadecimalNumberas an array of characters to store the hexadecimal digits of the converted number.ias an index variable to keep track of the position of each digit in thehexadecimalNumberarray.
- The
quotientis initially set to thedecimalNumber. - A loop is used to perform the conversion process:
- In each iteration, the
remainderis calculated as the remainder when thequotientis divided by 16. This step is performed using the modulus operator%. - If the
remainderis less than 10, it represents a digit between 0 and 9. In this case, the ASCII value of the digit is calculated by adding 48 to theremainder. The resulting character is stored in thehexadecimalNumberarray at indexi. - If the
remainderis greater than or equal to 10, it represents a digit between A and F. In this case, the ASCII value of the digit is calculated by adding 55 to theremainder(to account for the ASCII values of A, B, C, D, E, and F). The resulting character is stored in thehexadecimalNumberarray at indexi. - The
quotientis updated by dividing it by 16 to move on to the next iteration. - The loop continues until the
quotientbecomes 0, indicating that the conversion is complete.
- In each iteration, the
- After the loop, the resulting hexadecimal representation is printed by iterating through the
hexadecimalNumberarray in reverse order. This is done by starting the loop fromi-1and decrementingjuntil it becomes 0. The hexadecimal digits are printed as characters using theprintffunction.
- Variables are declared:
- In the
mainfunction:- An integer variable
decimalNumberis declared to store the decimal number entered by the user. - The user is prompted to enter a decimal number using the
printffunction. - The decimal number is read from the user using the
scanffunction and stored in thedecimalNumbervariable. - The
decimalToHexadecimalfunction is called with thedecimalNumberas an argument to perform the conversion and display the hexadecimal representation.
- An integer variable
By following these steps, the program takes a decimal number as input, converts it to its hexadecimal representation using the decimalToHexadecimal function, and displays the result to the user.
Input /Output

In this example, the program prompts the user to enter a decimal number. The user enters the number 172.
The program then converts the decimal number 172 to its hexadecimal representation using the decimalToHexadecimal function. The resulting hexadecimal representation is AC.
Finally, the program displays the hexadecimal representation to the user as AC.
You can run the program and try different decimal numbers to see their corresponding hexadecimal representations.