forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecimal_to_hexa.c
46 lines (29 loc) · 853 Bytes
/
decimal_to_hexa.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
/*****Decimal to Hexadecimal conversion*******************/
#include <stdio.h>
void decimal2Hexadecimal(long num);
int main(){
long decimalnum;
printf("Enter decimal number: ");
scanf("%ld", &decimalnum);
decimal2Hexadecimal(decimalnum);
return 0;
}
/********function for convert decimal number to hexadecimal number****************/
void decimal2Hexadecimal(long num){
long decimalnum=num;
long quotient, remainder;
int i, j = 0;
char hexadecimalnum[100];
quotient = decimalnum;
while (quotient != 0){
remainder = quotient % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
else
hexadecimalnum[j++] = 55 + remainder;
quotient = quotient / 16;}
// print the hexadecimal number
for (i = j; i >= 0; i--){
printf("%c", hexadecimalnum[i]);}
printf("\n");
}