diff --git a/cpp/decimal_to_binary.cpp b/cpp/decimal_to_binary.cpp new file mode 100644 index 000000000..ca1f50662 --- /dev/null +++ b/cpp/decimal_to_binary.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +int convertToBinary(int n){ + + int ans = 0; + int p = 1; + + while(n>0){ + int last_bit = (n&1); + ans += p*last_bit; + + p = p*10; + n = n>>1; + } + return ans; + +} + + +int main(){ + + int n; + cin>>n; + + cout << convertToBinary(n) <