Write a C program to read decimal number from user and convert to Hexadecimal number system. How to convert Decimal number system to Hexadecimal number system in C.

Input decimal number: (26)

Output hexadecimal number: (1A)

Happy coding ;)

**Example:**Input decimal number: (26)

_{10}Output hexadecimal number: (1A)

_{16}### Required knowledge:

Basic C programming, For loop#### Decimal number system:

Decimal number system is a base 10 number system. Decimal number system uses 10 symbols to represent all number i.e. 0 1 2 3 4 5 6 7 8 9#### Hexadecimal number system:

Hexadecimal number system is a base 16 number system. Hexadecimal number system uses 16 symbols to represent all numbers i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F### Algorithm to convert from Decimal to Hexadecimal:

Algorithm Conversion from Decimal to Hexadecimalbegin:read (DECIMAL);HEX← NULL;REM← 0;HEXVALUES[] ← 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F;While(DECIMAL!= 0)begin:REM←DECIMAL% 16;HEX←HEX+HEXVALUES[REM];DECIMAL←DECIMAL/ 16;end;Reverse(HEX); print('Hexadecimal = 'HEX);end;

### Program:

/** * C program to convert from Decimal number system to hexadecimal number system */ #include <stdio.h> #include <string.h> int main() { char HEXVALUE[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; long long decimal, tempDecimal; char hex[65]; int index, rem; /* * Reads decimal number from user */ printf("Enter any decimal number: "); scanf("%lld", &decimal); tempDecimal = decimal; index = 0; /* * Finds the hexadecimal of decimal number */ while(tempDecimal !=0) { rem = tempDecimal % 16; hex[index] = HEXVALUE[rem]; tempDecimal /= 16; index++; } hex[index] = '\0'; strrev(hex); printf("\nDecimal number = %lld\n", decimal); printf("Hexadecimal number = %s", hex); return 0; }

Output

Enter any decimal number: 427

Decimal number = 427

Hexadecimal number = 1AB

Decimal number = 427

Hexadecimal number = 1AB

Happy coding ;)

### You may also like

- Loop programming exercises and solutions in C.
- C program to find one's complement of a binary number.
- C program to find two's complement of a binary number.
- C program to convert Binary to Octal number system.
- C program to convert Binary to Decimal number system.
- C program to convert Binary to Hexadecimal number system.
- C program to convert Octal to Binary number system.
- C program to convert Octal to Decimal number system.
- C program to convert Octal to Hexadecimal number system.
- C program to convert Decimal to Binary number system.
- C program to convert Decimal to Octal number system.
- C program to convert Hexadecimal to Binary number system.
- C program to convert Hexadecimal to Octal number system.
- C program to convert Hexadecimal to Decimal number system.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).