Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

binary add using strings

#include <iostream>
#include <string>
using namespace std;

int DecToBin(int dec)
{
    long long bin = 0;
    int j,rem, i = 1;

    for(j=dec;j!=0;j)
    {
        rem = j % 2;
        j /= 2;
        bin += rem * i;
        i *= 10;
    }

    return bin;
}

int main()
{
    string number_1, number_2;
    int decimal_sum;

    cout<< "Enter first number: ";
    cin>> number_1;

    cout<< "Enter second number: ";
    cin>> number_2;

    decimal_sum = stoi(number_1 , 0 ,2) + stoi(number_2 , 0 ,2);

    cout<< "Binary Sum = " <<DecToBin(decimal_sum);

    return 0;
}
 
PREVIOUS NEXT
Tagged: #binary #add #strings
ADD COMMENT
Topic
Name
6+4 =