-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDec2Bin.CPP
82 lines (81 loc) · 1.18 KB
/
Dec2Bin.CPP
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
#include <iostream>
using namespace std;
#define MAX 32
class STACK
{
int a[MAX];
int top;
public:
STACK()
{
top = -1;
}
bool push(int x);
int pop();
bool isEmpty();
int peek();
} s;
bool STACK::push(int x)
{
if (top > MAX)
{
cout << "\nStack overflow!";
}
else
{
a[++top] = x;
}
}
int STACK::pop()
{
if (top < 0)
{
cout << "\nStack Underflow!";
}
else
{
int x = a[top--];
return x;
}
}
int STACK ::peek()
{
if (top < 0)
{
cout << "\nThe stack is empty!" << endl;
}
else
{
int x = a[top];
return x;
}
}
bool STACK ::isEmpty()
{
return (top < 0);
}
void dec2bin(int n)
{
int rem;
while (n >= 2)
{
rem = n % 2;
s.push(rem);
n = n / 2;
}
s.push(n);
cout << "The binary conversion of number is:" << endl;
while (!s.isEmpty())
{
cout << s.peek();
s.pop();
}
}
int main()
{
int num;
cout << "Enter a number:" << endl;
cin >> num;
dec2bin(num);
return 0;
}