1
2// C++ program to convert a decimal
3// number to binary number
4
5#include <iostream>
6using namespace std;
7
8// function to convert decimal to binary
9void decToBinary(int n)
10{
11 // array to store binary number
12 int binaryNum[32];
13
14 // counter for binary array
15 int i = 0;
16 while (n > 0) {
17
18 // storing remainder in binary array
19 binaryNum[i] = n % 2;
20 n = n / 2;
21 i++;
22 }
23
24 // printing binary array in reverse order
25 for (int j = i - 1; j >= 0; j--)
26 cout << binaryNum[j];
27}
28
29// Driver program to test above function
30int main()
31{
32 int n = 17;
33 decToBinary(n);
34 return 0;
35}
1// C++ program for decimal to binary
2
3#include <iostream>
4#include <algorithm>
5#include <string>
6#include <vector>
7
8
9using namespace std;
10
11int main() {
12
13 vector<int>nums; // list that will hold binary values
14
15 int num = 0;
16 cout<<"Number: "<<endl;
17 cin>>num; // number input
18 int i=0; // iterator for vector
19
20 while(num!=0)
21 {
22 nums.push_back(num%2); // adds binary value to the back of string
23 i++; // i gets incremented for the next position in vector
24 num=num/2;
25 }
26
27 reverse(nums.begin(),nums.end()); // reverses order of vector
28
29 for(auto x:nums)
30 {
31 cout<<x; // outputs stuff in vector
32 }
33 return 0;
34}