-
Notifications
You must be signed in to change notification settings - Fork 0
/
110.c
102 lines (90 loc) · 2.25 KB
/
110.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* C program to convert Octal to Hexadecimal number system
*/
#include <stdio.h>
int main()
{
int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};
long long octal, tempOctal, binary, place;
char hex[65] = "";
int rem;
place = 1;
binary = 0;
/* Input octal number from user */
printf("Enter any octal number: ");
scanf("%lld", &octal);
tempOctal = octal;
/*
* Octal to binary conversion
*/
while(tempOctal > 0)
{
rem = tempOctal % 10;
binary = (OCTALVALUES[rem] * place) + binary;
tempOctal /= 10;
place *= 1000;
}
/*
* Binary to hexadecimal conversion
*/
while(binary > 0)
{
rem = binary % 10000;
switch(rem)
{
case 0:
strcat(hex, "0");
break;
case 1:
strcat(hex, "1");
break;
case 10:
strcat(hex, "2");
break;
case 11:
strcat(hex, "3");
break;
case 100:
strcat(hex, "4");
break;
case 101:
strcat(hex, "5");
break;
case 110:
strcat(hex, "6");
break;
case 111:
strcat(hex, "7");
break;
case 1000:
strcat(hex, "8");
break;
case 1001:
strcat(hex, "9");
break;
case 1010:
strcat(hex, "A");
break;
case 1011:
strcat(hex, "B");
break;
case 1100:
strcat(hex, "C");
break;
case 1101:
strcat(hex, "D");
break;
case 1110:
strcat(hex, "E");
break;
case 1111:
strcat(hex, "F");
break;
}
binary /= 10000;
}
strrev(hex);
printf("Octal number: %lld\n", octal);
printf("Hexadecimal number: %s", hex);
return 0;
}