Advertisement
fueanta

Dec to BIN

Mar 2nd, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.34 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement