Created
January 11, 2016 14:21
-
-
Save RezaBidar/de4dff64bbb3c35c91c2 to your computer and use it in GitHub Desktop.
add 2 binary value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cmath> | |
using namespace std; | |
int decimal_binary(int n) /* Function to convert decimal to binary.*/ | |
{ | |
int rem, i = 1, binary = 0; | |
while (n != 0) | |
{ | |
rem = n % 2; | |
n /= 2; | |
binary += rem*i; | |
i *= 10; | |
} | |
return binary; | |
} | |
int binary_decimal(int n) /* Function to convert binary to decimal.*/ | |
{ | |
int decimal = 0, i = 0, rem; | |
while (n != 0) | |
{ | |
rem = n % 10; | |
n /= 10; | |
decimal += rem*pow(2, i); | |
++i; | |
} | |
return decimal; | |
} | |
int main() | |
{ | |
int a, b; | |
cin >> a >> b; | |
cout << decimal_binary( binary_decimal(a) + binary_decimal(b)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment