 # C Programming Code Examples

## C > Conversions Code Examples

### C program to Convert Decimal to Hexadecimal

``` C program to Convert Decimal to Hexadecimal This program takes a decimal number as input and converts to hexadecimal. Take a decimal number as input. Divide the input number by 16. Store the remainder in the array. Do step 2 with the quotient obtained until quotient becomes zero. Print the array in the reversed fashion to get hexadecimal number. #include <stdio.h> int main() { long decimalnumber, quotient, remainder; int i, j = 0; char hexadecimalnumber; printf("Enter decimal number: "); scanf("%ld", &decimalnumber); quotient = decimalnumber; while (quotient != 0) { remainder = quotient % 16; if (remainder < 10) hexadecimalnumber[j++] = 48 + remainder; else hexadecimalnumber[j++] = 55 + remainder; quotient = quotient / 16; } // display integer into character for (i = j; i >= 0; i--) printf("%c", hexadecimalnumber[i]); return 0; } Take a decimal number as input and store it in the variable decimalnumber. Initialize the variable j=0 and copy decimalnumber to variable quotient. Obtain the quotient and remainder of the variable quotient. Store the obtained remainder in the variable remainder and override the variable quotient with obtained quotient. Check if the remainder is less than 10. If it is, then add 48 to the remainder and store the result in the array hexadecimalnumber. Otherwise, add 55 to the remainder and store the result in the array hexadecimalnumber. Do steps 3-4 until variable quotient becomes zero. When it becomes zero, print the array hexadecimalnumber in the reversed fashion as output. ```