Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am working on building a binary calculator and I've set up binary addition with the code shown first. I am having trouble with the
I am working on building a binary calculator and I've set up binary addition with the code shown first. I am having trouble with the subtraction and multiplication code shown last last. I just noticed i'm missing an = sign at // if (c = 1) { so i will change that. Is there anything else I should change or add to complete the binary calculator?
Here is the addition code:
#include#include using namespace std; int main() { long a, b; int i = 0, r = 0, sum[20]; cout << "Enter the first binary number: "; cin >> a; cout << "Enter the second binary number: "; cin >> b; while (a != 0 || b != 0) { sum[i++] = (a % 10 + b % 10 + r) % 2; r = (a % 10 + b % 10 + r) / 2; a = a / 10; b = b / 10; } if (r != 0) sum[i++] = r; --i; cout << "The sum of the two binary numbers is: "; while (i >= 0) cout << sum[i--]; cout << ". "; system("pause"); return 0; }
Here is the subtraction and division code still with the missing = sign i will fix.
#include#include using namespace std; int main() { long a, b; int c = 0; cout << "Enter the first binary number: "; cin >> a; cout << "Enter the second binary number: "; cin >> b; cout << "Which operation do you want to complete: Add=1, Subtract=2, Multiply=3: "; cin >> c; if (c = 1){ while (a != 0 || b != 0) { int i = 0, r = 0, sum[20]; sum[i++] = (a % 10 + b % 10 + r) % 2; r = (a % 10 + b % 10 + r) / 2; a = a / 10; b = b / 10; } if (r != 0) sum[i++] = r; --i; cout << "The sum of the two binary numbers is: "; while (i >= 0) cout << sum[i--]; cout << ". "; } if (c = 2) { while (a != 0 || b != 0) { int i = 0, r = 0, sub[20]; sub[i++] = (a % 10 - b % 10 + r) % 2; r = (a % 10 - b % 10 + r) / 2; a = a / 10; b = b / 10; if (r != 0) sub[i++] = r; --i; cout << "The difference of the two binary numbers is: "; while (i >= 0) cout << sub[i--]; cout << ". "; } if (c = 3) { while (a != 0 || b != 0) { int i = 0, r = 0, prod[20]; prod[i++] = (a % 10 * b % 10 + r) % 2; r = (a % 10 * b % 10 + r) / 2; a = a / 10; b = b / 10; if (r != 0) prod[i++] = r; --i; cout << "The product of the two binary numbers is: "; while (i >= 0) cout << prod[i--]; cout << ". "; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started