Advertisement
Guest User

decimal to binary

a guest
Sep 30th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <vector>
  7. #include <string>
  8. #include <algorithm>
  9. #include <sstream>
  10. #include <iterator>
  11.  
  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14. std::cout << "Decimal to Binary Conversion 1.0" << std::endl;
  15. std::cout << "\n";
  16. int decimalValue;
  17. std::cin >> decimalValue;
  18. std::vector<int> binaryDigits;
  19. bool conversion = true;
  20.  
  21. int n = decimalValue;
  22. int b;
  23. std::cout << "Converting..." << std::endl;
  24. do{
  25. if(!n <= 0)
  26. {
  27. b = n % 2;
  28. binaryDigits.push_back(b);
  29. std::cout << ".";
  30. n = n/2;
  31. }
  32. else
  33. conversion = false;
  34. }while(conversion);
  35.  
  36. std::cout << std::endl;
  37. std::cout << "Conversion Complete" << std::endl;
  38.  
  39. std::string out(binaryDigits.rbegin(), binaryDigits.rend());
  40.  
  41. std::cout << "The binary conversion of " << decimalValue << " is: ";
  42.  
  43. for( std::vector<int>::reverse_iterator iter = binaryDigits.rbegin(); iter != binaryDigits.rend(); ++iter)
  44. std::cout << *iter;
  45.  
  46. std::cout << std::endl;
  47. system("pause");
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement