-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_decimal_binary_octal_hexa.c
60 lines (43 loc) · 1.16 KB
/
convert_decimal_binary_octal_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
int main() {
int decimal;
// printf("Enter a decimal number: ");
scanf("%d", &decimal);
if (decimal <= 0) {
printf("Error: Value should be greater than 0");
}
else {
printf("Binary equivalent: ");
decToBinary(decimal);
printf("\nOctal equivalent: ");
decToOctal(decimal);
printf("\nHexadecimal equivalent: ");
decToHexadecimal(decimal);
printf("\n");
}
return 0;
}
int decToBinary(int decimal) {
if (decimal > 0) {
decToBinary(decimal / 2);
printf("%d", decimal % 2);
}
}
void decToHexadecimal(int decimal) {
if (decimal > 0) {
decToHexadecimal(decimal / 16);
int remainder = decimal % 16;
if (remainder < 10) {
printf("%d", remainder);
}
else {
printf("%c", 'A' + remainder - 10);
}
}
}
void decToOctal(int decimal) {
if (decimal > 0) {
decToOctal(decimal / 8);
printf("%d", decimal % 8);
}
}