cpp package to convert decimal into binary code example

Example: how to do decimal to binary converdsion in c++

// C++ program for decimal to binary 

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>


using namespace std; 

int main() {

    vector<int>nums; // list that will hold binary values

    int num = 0; 
    cout<<"Number: "<<endl;
    cin>>num; // number input 
    int i=0; // iterator for vector

    while(num!=0)
    {
        nums.push_back(num%2); // adds binary value to the back of string 
        i++; // i gets incremented for the next position in vector 
        num=num/2; 
    }
 
    reverse(nums.begin(),nums.end()); // reverses order of vector 
  
    for(auto x:nums)
    {
        cout<<x; // outputs stuff in vector 
    }
  return 0; 
}

Tags:

Cpp Example