Conversion of Decimal numbers to Binary

This program is almost complete, although it performs its function, that is, it converts base 10 number to base 2. However, I want it to be able to request from the user, to enter numbers for conversion until the user hit ESC key to exit the program.

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>


using namespace std;

int main()
{
	int num;
	int bit1 = 0;
	int bit2 = 1;

	vector<int> iNum;

             cout<< "\n\nEnter an integer value (Enter to exit): \n";
             cin>> num;

         do
            {
            if(num%2 == bit1)
                iNum.push_back(bit1);
            else if (num%2 == bit2 || num == bit2 || num%2<bit2)
                iNum.push_back(bit2);                             
              else
                iNum.push_back(bit2);

                num =  num/2;
            }while (num>0);

       int size = iNum.size();

    cout<< "The computed binary value of the input integer " << num << " is: ";
            int number = num;
           
        for (int j=0; j<iNum.size(); ++j)

           {
                cout<< "\n" <<iNum[j];
           }

            cout << endl;

            int k=iNum.size();
            cout<< "The number is " << k << " bits \n\n" ;

        cout<<"The binary value of the decimal value " << number << " is: ";
        for(k=iNum.size(); k>0; k--)
            {
                cout<< iNum[k-1];
            }

            cout<< endl;
}
Last edited on
Topic archived. No new replies allowed.