fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string to_binary(int n){
  5. if (n == 0) return "0";
  6.  
  7. string res;
  8. while(n){
  9. if(n % 2 == 1){
  10. res += '1';
  11. } else {
  12. res += '0';
  13. }
  14. n /= 2;
  15. }
  16. reverse(res.begin(), res.end()); // To get MSB to LSB
  17. return res;
  18. }
  19.  
  20. int main() {
  21. string res = to_binary(7);
  22. cout << res;
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5316KB
stdin
7
0
662573
10000
18898541
32385559
4
6
stdout
111