Challenge : Binary Addition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

Challenge : Binary Addition

Write a code to first convert two numbers to binary and then add them. Note : No cheating allowed! Don't directy add in decimal and convert to binary! Enjoy! Happy coding

11th Oct 2017, 1:19 PM
Infinity
Infinity - avatar
16 Answers
11th Oct 2017, 8:59 PM
LukArToDo
LukArToDo - avatar
+ 22
Addition without a HEADACHE! This is not what you want, but as a straightforward solution can be considered. #include <iostream> #include <bitset> using namespace std; #define bin(x) bitset<16>(x).to_string() int main() { int n1 = 0; int n2 = 0; // unsigned max range = 65535 // signed min range = -32768 cout << "Enter n1: "; cin >> n1; do { cout << "Enter n2: "; cin >> n2; } while (n1 + n2 > 65535 || n1 + n2 < -32768); cout << endl; cout << "n1 Bin : " << bin(n1) << endl; cout << "n2 Bin : " << bin(n2) << endl; cout << "n1 + n2: " << bin(n1 + n2) << endl; } [https://code.sololearn.com/c9lLDE93zjar]
11th Oct 2017, 3:25 PM
Babak
Babak - avatar
+ 5
I'll make it .
11th Oct 2017, 1:35 PM
RZK 022
RZK 022 - avatar
12th Oct 2017, 1:23 AM
Calviղ
Calviղ - avatar
+ 3
this program runs a logical and & logical or and add them to get the results . although it might not be according to demand of question. you can check out this java program as an alternative to add 2 nos. https://code.sololearn.com/czOSdOkvHLc6/?ref=app
12th Oct 2017, 10:48 AM
Nilesh Tanwar
Nilesh Tanwar - avatar
+ 3
I have challenge ready for u too!! Check this out if u love web!! https://www.sololearn.com/discuss/793894/?ref=app
17th Oct 2017, 3:15 PM
Infinity
Infinity - avatar
+ 2
I made it but , it first adds the number then converts it to binary . I will think over the algorithm after a while , but till then here's what I created : https://code.sololearn.com/WxzMXi4EdBtX/?ref=app
11th Oct 2017, 2:45 PM
RZK 022
RZK 022 - avatar
+ 2
Although I did not understand clearly whether to convert the numbers into binary and then return their sum in binary or return their sum as as integer. Here's what I came up with. Is it correct? https://code.sololearn.com/coaqoWuWP3aS/?ref=app
12th Oct 2017, 12:41 PM
Amandeep Singh
Amandeep Singh - avatar
13th Oct 2017, 9:18 AM
Hiroki Masuda
Hiroki Masuda - avatar
+ 1
#include<stdio.h> #include<conio.h> #include<string.h> int main() { char sum[100]="";int c=0; char str[100]; char str2[200];char string[100]={0};printf("attention guys the following program is meant fr onl binary code so do be careful while entering a value\n\n"); printf("enter first binary no\n"); scanf("%s",str); printf("\nenter second binary no\n"); scanf("%s",str2); int l1=strlen(str)-1;int l2=strlen(str2)-1; int m=(l1>l2)?l1:l2; while((l1>=0)&&(l2>=0)) { char s1=str[l1];char s2=str2[l2]; if((s1=='1')&&(s2=='1')) { if(c==1) { strcat(sum,"1");c=1; } else { strcat(sum,"0"); c=1; } }//if else if((s1=='0')&&(s2=='0')) { if(c==1) { strcat(sum,"1");c=0; } else { strcat(sum,"0"); c=0; } }//else if else { if(c==1) { strcat(sum,"0");c=1; } else { strcat(sum,"1"); c=0; } }l1--;l2--; }printf("%s",sum);//while if(strlen(str)!=strlen(str2)) { if(l1>=0) { for(int i=l1;i>=0;i--) { char s=str[i]; if((s=='1')&&(c==1)) { strcat(sum,"0");c=1; } else if((s=='0')&&(c==0)) { strcat(sum,"0");c=0; } else { strcat(sum,"1");c=0; } }//for } //if else { for(int i=l2;i>=0;i--) { char s=str2[i]; if((s=='1')&&(c==1)) { strcat(sum,"0");c=1; } else if((s=='0')&&(c==0)) { strcat(sum,"0");c=0; } else { strcat(sum,"1");c=0; } }//for } } int a=0;//else for(int i=strlen(sum)-1;i>=0;i--) string[a++]=sum[i]; if(c==1) printf("binary addition of %s and %s is : %s ",str,str2,strcat("1",string)); else printf("binary addition of %s and %s is : %s ",str,str2,strcat("0",string)); }
31st Dec 2017, 2:27 AM
Phurba Sherpa
Phurba Sherpa - avatar
0
I don’t get c++
12th Oct 2017, 2:02 PM
Carl
12th Oct 2017, 4:50 PM
arumuga priya
arumuga priya - avatar
0
here's my code. No cheating. No shortcuts. from decimal inputs->binary->add as binary->convert result back to decimal. https://code.sololearn.com/WJmDbUuIB08R/?ref=app
12th Oct 2017, 11:17 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
- 1
no problem Mr you can.
12th Oct 2017, 1:41 PM
mostafa
mostafa - avatar
- 1
HERES A NEW CHALLENGE CHECK IT AND TRY IF YOU WANT https://www.sololearn.com/discuss/793924/?ref=app
17th Oct 2017, 3:13 PM
sayan chandra
sayan chandra - avatar
- 4
Are you aware most programming languages already add numbers in binary by default? They might be displayed and input in decemal, but the internal storage and operations are done in binary. (I think I know what you mean, but can't help thinking you can't add numbers in decimal form anyway)
11th Oct 2017, 8:59 PM
Jared Bird
Jared Bird - avatar