-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdecToBinStack.cpp
More file actions
46 lines (35 loc) · 920 Bytes
/
decToBinStack.cpp
File metadata and controls
46 lines (35 loc) · 920 Bytes
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
#include<iostream>
#include<conio.h>
#include<string>
#include<stack>
using namespace std;
string decimalToBinary(int decimalNumber) {
string binaryNumber = "";
int temp = decimalNumber;
stack <int> s;
while (temp !=0) {
s.push(temp%2);
temp /= 2;
}
while (!s.empty()) {
binaryNumber += (char)s.top()+48;
s.pop();
}
return binaryNumber;
}
int main() {
string binaryNumber;
int decimalNumber;
char choice = 'y';
cout << "\t\t***** Decimal To Binary*****\n";
do {
cout << "Enter a positive number: ";
cin >> decimalNumber;
binaryNumber = decimalToBinary(decimalNumber);
cout << "The corresponding binary equivalent of " << decimalNumber << " is " << binaryNumber;
cout << "\n\nWant to enter a new number: ";
cin >> choice;
}while(choice == 'y');
getch();
return 0;
}