Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10.     cout << "Enter decimal number: ";
  11.     int denary;
  12.     cin >> denary;
  13.    
  14.     int tempDenary = denary;
  15.     vector<int> binary;
  16.  
  17.     // Turn denary into binary
  18.     while (tempDenary >0) {
  19.  
  20.         if (tempDenary % 2 == 0) {
  21.             binary.push_back(0);
  22.             tempDenary = tempDenary / 2;
  23.         }
  24.  
  25.         if (tempDenary % 2 == 1) {
  26.             binary.push_back(1);
  27.             tempDenary = tempDenary / 2;
  28.         }
  29.     }
  30.  
  31.     cout << binary[0];
  32.     cout << binary[1];
  33.     cout << binary[2];
  34.     cout << binary[3];
  35.     cout << binary[4];
  36.     cout << binary[5];
  37.  
  38.     /*
  39.     for (int i = 0; i < size; i++) {
  40.         cout << binary[i];
  41.     }
  42.     */
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement