-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdectobin.c
133 lines (117 loc) · 1.91 KB
/
dectobin.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#define MAX 100
int stack[MAX], top = -1; // stack array and top
// Push function to insert element
void push(int data)
{
if (top == MAX - 1)
printf("\nStack overflow");
else
stack[++top] = data;
}
// Pop function to pop element
int pop()
{
int del;
if (top == -1)
{
printf("\nStack empty");
return -1;
}
else
{
del = stack[top--];
return del;
}
}
// Displays top element without popping it
int peek()
{
if(top == -1)
return -1;
else
return stack[top];
}
// To print elements of stack
void display()
{
if (top == -1)
printf("\nStack empty");
else
{
printf("\n");
for (int i = 0; i <= top; i++)
{
printf("%d, ", stack[i]);
}
}
}
void convertToBinary(int num)
{
while (num)
{
push(num%2);
num /= 2;
}
}
void displayBinary()
{
printf("\nBinary : ");
while(top != -1)
{
printf("%d",pop());
}
printf("\n");
}
void convertToHex(int num)
{
while (num)
{
push(num%16);
num /= 16;
}
}
void displayHex()
{
printf("\nHex : ");
while(top != -1)
{
int disp = pop();
switch (disp)
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
default:
printf("%d",disp);
break;
}
}
printf("\n\n");
}
int main()
{
int num;
printf("Enter a Number: ");
scanf("%d",&num);
convertToBinary(num);
displayBinary();
convertToHex(num);
displayHex();
return 0;
}