+ 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
16 Respuestas
+ 9
+ 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]
+ 5
I'll make it .
+ 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
+ 3
I have challenge ready for u too!!
Check this out if u love web!!
https://www.sololearn.com/discuss/793894/?ref=app
+ 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
+ 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
+ 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));
}
0
I don’t get c++
0
I have got up the result
https://code.sololearn.com/cIm97o4kK8e4/?ref=app
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
- 1
no problem Mr
you can.
- 1
HERES A NEW CHALLENGE
CHECK IT AND TRY IF YOU WANT
https://www.sololearn.com/discuss/793924/?ref=app
- 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)