From 708945f94a2294c585fd7f5fcaa6a242f79d7b03 Mon Sep 17 00:00:00 2001 From: Devansh Dwivedi <74648201+bottercode@users.noreply.github.com> Date: Fri, 29 Oct 2021 23:36:36 +0530 Subject: [PATCH] Add files via upload --- cpp/decimal_to_binary.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 cpp/decimal_to_binary.cpp 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) <