diff --git a/binary_add.cpp b/binary_add.cpp new file mode 100644 index 0000000..67ee8b0 --- /dev/null +++ b/binary_add.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + +int reverse(int n){ + int ans = 0; + while(n>0){ + int l = n % 10; + ans = ans *10 + l; + n /=10; + } + return ans; +} +int binarysum(int a,int b){ + int ans = 0, prevcarry = 0; + while(a > 0 && b > 0){ + if(a%2 == 0 && b%2 == 0){ + ans = ans*10 + prevcarry; + prevcarry = 0; + } + else if((a%2 == 0 && b%2 == 1) || (a%2 == 1 && b%2 == 0)){ + if(prevcarry == 1){ + ans = ans*10 + 0; + prevcarry = 1; + } + else{ + ans = ans*10 + 1; + prevcarry = 0; + } + } + else{ + ans = ans*10 + prevcarry; + prevcarry = 1; + } + + a/=10; + b/=10; +} + + while(a > 0){ + if(prevcarry == 1){ + if(a%2 == 1){ + ans = ans*10 + 0; + prevcarry = 1; + } + else{ + ans = ans*10 + 1; + prevcarry = 0; + } + } + else{ + ans = ans*10 + (a%2); + } + a/=10; + } +while(b > 0){ + if(prevcarry == 1){ + if(b%2 == 1){ + ans = ans*10 + 0; + prevcarry = 1; + } + else{ + ans = ans*10 + 1; + prevcarry = 0; + } + } + else{ + ans = ans*10 + (b%2); + } + b/=10; + } + if(prevcarry == 1){ + ans = ans*10 + 1; + } + ans = reverse(ans); + return ans; +} + +int main(){ + int a,b; + cin >> a >> b; + cout << binarysum(a,b) << endl; + +return 0; +} diff --git a/decimaltobinary.cpp b/decimaltobinary.cpp new file mode 100644 index 0000000..085220c --- /dev/null +++ b/decimaltobinary.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +// function to convert decimal to binary +void decToBinary(int n) +{ + // array to store binary number + int binaryNum[32]; + + // counter for binary array + int i = 0; + while (n > 0) { + + // storing remainder in binary array + binaryNum[i] = n % 2; + n = n / 2; + i++; + } + + // printing binary array in reverse order + for (int j = i - 1; j >= 0; j--) + cout << binaryNum[j]; +} + +// Driver program to test above function +int main() +{ + int n = 17; + decToBinary(n); + return 0; +}