Question

In: Computer Science

c++ Create a program where you can add binary while they're still in binary form and...

c++

Create a program where you can add binary while they're still in binary form and convert them to decimal, adds them, then converts then back to binary. (without built in conversions).

Convert 34 to Binary Convert 22 to Binary Convert 1001 to Decimal Convert 111011 to Decimal Add 11 and 1110 in Binary Return the Answer in Binary Subtract 111 from 1010 in Binary, then Convert the Answer to Decimal

Solutions

Expert Solution

#include <iostream>

using namespace std;

int toDecimal(long n)
{
long num = n;
int decimalNum =0;
int remaind;
int base =1;
while (num > 0)
{
remaind = num % 10;
decimalNum = decimalNum + remaind * base;
num = num / 10 ;
base = base * 2;
}
return decimalNum;
}

long toBinary(int n)
{
int num = n;
long binaryNum = 0;
int remaind, i = 1;
  
while (num!=0)
{
remaind = num%2;

num = num/2;
binaryNum = binaryNum + remaind*i;
i = i * 10;
}
return binaryNum;
}

int main()
{
  
long binary1 = 101010;
long binary2 = 111111;
  
cout<<"Calculating sum of two binary Numbers "<<endl;
  
int sum = toDecimal(binary1)+toDecimal(binary2);
  
cout<<"Sum of two binary numbers in Decimal is : "<<sum<<endl;
  
long sumBinary = toBinary(sum);
  
cout<<"Sum of two binary numbers in Binary is : "<<sumBinary<<endl;
  
  

return 0;
}

Output

Calculating sum of two binary Numbers 
Sum of two binary numbers in Decimal is : 105
Sum of two binary numbers in Binary is : 1101001

Screenshot

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful


Related Solutions

How would I create a program in C++ where you build and maintain two binary search...
How would I create a program in C++ where you build and maintain two binary search trees of information? I have already created the file reader which I will list on the end. The 2 binary search trees should be controlled by the Operations: L -- for launching a satellite, which will save it's info to the first set or D -- for deorbit the satellite. The other operations I'm looking to implement are: F -- for find, where user...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
C++ program to accept characters as a polynomial and then output whether they're valid or not....
C++ program to accept characters as a polynomial and then output whether they're valid or not. Types of polynomials that don't work: n^5n, n^9.7, 8n, n^7-7*n
C/ C++ Preferably 1. Write a simple program where you create an array of single byte...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++)...
code in c++ using the code given add a hexadecimal to binary converter and add a...
code in c++ using the code given add a hexadecimal to binary converter and add a binary to hexadecimal converter #include <iostream> #include <string> #include<cmath> #include<string> using namespace std; int main() { string again; do { int userChoice; cout << "Press 2 for Decimal to Binary"<< endl; cout << "Press 1 for Binary to Decimal: "; cin >> userChoice; if (userChoice == 1) { long n; cout << "enter binary number" << endl; cin>>n; int decnum=0, i=0, remainder; while(n!=0) {...
In this question using c++, you have to design the Pascal triangle where you can create...
In this question using c++, you have to design the Pascal triangle where you can create a list of coefficients of a binomial of any size that is passed to the constructor. Data members and member functions of this class are of your design choice. You need to use only pointers and pointers to pointers (do not use regular array notations). Create a two-dimensional array in the heap memory (which creates a dynamic memory). Use an application program to print...
create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT