fueanta

Dec to BIN

Mar 2nd, 2017
133
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // decimal to binary
  2.  
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10.     int num;
  11.     cout << "Decimal: ";
  12.     cin >> num;
  13.     string binNum;
  14.     int x = num;
  15.  
  16.     while (x > 0) {
  17.         binNum.insert(0, to_string(x % 2));
  18.         x /= 2;
  19.     }
  20.  
  21.     cout << "Binary representation of " << num << " is " << binNum << endl;
  22.  
  23.     return 0;
  24. }
RAW Paste Data