dstamatova

DecimalToBinary

Nov 26th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. // Alg-dec-bin.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. using namespace std;
  7.  
  8.  
  9. vector <int> decimalToBinary(int num)
  10. {
  11.     vector <int> vResult = {};
  12.     while (num)
  13.     {
  14.         vResult.push_back (num % 2);
  15.         num /= 2;
  16.     }
  17.     return vResult;
  18. }
  19.  
  20. int main()
  21. {
  22.     int num;
  23.     cin >> num;
  24.     vector <int> vBinary = decimalToBinary(num);
  25.  
  26.     for (int i = vBinary.size() - 1; i >= 0; i--)
  27.     {
  28.         cout << vBinary[i];
  29.     }
  30. }
  31.  
  32.  
  33.  
Advertisement
Add Comment
Please, Sign In to add comment