Answers for "convert decimal to binary in c++"

C++
5

convert decimal to binary in c++

// C++ program to convert a decimal 
// number to binary number 
  
#include <iostream> 
using namespace std; 
  
// function to convert decimal to binary 
void decToBinary(int n) 
{ 
    // array to store binary number 
    int binaryNum[32]; 
  
    // counter for binary array 
    int i = 0; 
    while (n > 0) { 
  
        // storing remainder in binary array 
        binaryNum[i] = n % 2; 
        n = n / 2; 
        i++; 
    } 
  
    // printing binary array in reverse order 
    for (int j = i - 1; j >= 0; j--) 
        cout << binaryNum[j]; 
} 
  
// Driver program to test above function 
int main() 
{ 
    int n = 17; 
    decToBinary(n); 
    return 0; 
}
Posted by: Guest on June-25-2020
2

convert decimal to binary c++

- Convert decimal to binary string using std::bitset
int n = 10000;
string s = bitset<32>(n).to_string(); // 32 is size of n (int)
Posted by: Guest on July-13-2021
0

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; 
}
Posted by: Guest on December-30-2020

Code answers related to "convert decimal to binary in c++"

Browse Popular Code Answers by Language