-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecToHex.c
46 lines (41 loc) · 931 Bytes
/
DecToHex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// decimal to hexadecimal
#include <stdio.h>
int main()
{
int number, cnt, i;
char hex[32];
printf("Enter decimal number: ");
scanf("%d", &number);
cnt = 0;
while (number > 0) {
switch (number % 16) {
case 10:
hex[cnt] = 'A';
break;
case 11:
hex[cnt] = 'B';
break;
case 12:
hex[cnt] = 'C';
break;
case 13:
hex[cnt] = 'D';
break;
case 14:
hex[cnt] = 'E';
break;
case 15:
hex[cnt] = 'F';
break;
default:
hex[cnt] = (number % 16) + 0x30;
}
number = number / 16;
cnt++;
}
printf("Hexadecimal value is: ");
for (i = (cnt - 1); i >= 0; i--)
printf("%c", hex[i]);
printf("\n");
return 0;
}