fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <string>
  6.  
  7. double dec2Bin(int value, char binaryString[])
  8. {
  9. int x = 1;
  10. std::string hold = "";
  11. while(x <= value){
  12. x *= 2;
  13. }
  14. x /= 2;
  15.  
  16. while(x >= 1){
  17. //cout << x << " ";
  18. if(value > x){
  19. hold += "1";
  20. value -= x;
  21. }
  22. else if(value < x){
  23. hold += "0";
  24. }
  25. else if(value == x){
  26. hold += "1";
  27. value = 0;
  28. //return hold;
  29. }
  30. x /= 2;
  31.  
  32. //cout << hold << endl;
  33. }
  34. return atoi(hold.c_str());
  35.  
  36. }
  37. int main()
  38. {
  39. char binstr[100];
  40. int num = 0;
  41. std::cout << "Enter a decimal string: ";
  42. std::cin >> num;
  43. std::cout << "its "<<dec2Bin(num, binstr) << std::endl;
  44.  
  45. }
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
Enter a decimal string: its 0