Decimal to hexadecimal conversion in C:
We can convert any decimal number (base-10 (0 to 9)) into hexadecinal number(base-16 (0 or 9 and A(10),B(11),C(12),D(13),E(14),F(15)) by c program.
Decimal Number
Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.
Hexadecimal Number
Hexadecimal number is a base 16 number because it ranges from 0 or 9 and A(10),B(11),C(12),D(13),E(14),F(15). Any combination of digits is Hexadecimal number such as A34, 5A4, 7DE7, ABCD etc.
To understand this example, you should have the knowledge of the following C programming topics:
C Functions
C User-defined functions
/* * C program to Convert Decimal to Hexadecimal */ #include <stdio.h> int main() { long decimalnum, quotient, remainder; int i, j = 0; char hexadecimalnum[100]; printf("Enter decimal number: "); scanf("%ld", &decimalnum); quotient = decimalnum; while (quotient != 0) { remainder = quotient % 16; if (remainder < 10) hexadecimalnum[j++] = 48 + remainder; else hexadecimalnum[j++] = 55 + remainder; quotient = quotient / 16; } // display integer into character for (i = j-1; i >= 0; i--) printf("%c", hexadecimalnum[i]); return 0; }
Output:
Enter decimal number: 159
9F
Enter decimal number: 37
25
Programming Solutions @ rajeshshukla
Tutorials | Technical Questions | Interview Questions |
---|---|---|
C Programming C++ Programming Class 11 (Python) Class 12 (Python) |
C Language C++ Programming Python |
C Interview Questions C++ Interview Questions |