1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Naddik [55]
3 years ago
6

Programming challenge description: Write a program that, given two binary numbers represented as strings, prints their sum in bi

nary. The binary strings are comma separated, two per line. The final answer should not have any leading zeroes. In case the answer is zero, just print one zero i.e. 0 Input: Your program should read lines from standard input. Each line contains two binary strings, separated by a comma and no spaces. Output: For each pair of binary numbers print to standard output their binary sum, one per line.
Computers and Technology
1 answer:
Lubov Fominskaja [6]3 years ago
8 0

Answer:

The program to this question can be given as:

Program:

#include<iostream>  //include header file

using namespace std;  

string add(string a, string b)  //define add function

{       //declare variable  

   string result = "";  

   int sum = 0,k,l;      

   cahr x;    

   k= a.size() - 1;

   l = b.size() - 1;  

   while (k>= 0 || l>= 0 || sum == 1) //loop

   {

   //addition 

       sum= sum+ ((k >= 0)? a[k] - '0': 0);  

       sum= sum+ ((l >= 0)? b[l] - '0': 0);

        x=sum%2;

      result=(x+'0') +result;

  // Compute carry

       sum =sum/ 2;  

       // Move to next digits  

       k--;  

       l--;  

   }  

   return result; //return value

}

int main()  //main method  

{  

   string a,b;  //string variable

   cout<<"Enter first binary digit: ";  //message.

   cin>>a;     //input from user

   cout<<"Enter Second binary digit: ";  //message.

   cin>>b;     //input from user

   cout <<"Addition :"<<add(a, b)<<endl; //print addition

   return 0;  

}

Output:

Enter first binary digit: 1101

Enter Second binary digit: 100

Addition :10001

Explanation:

In the above c++ program first, we include the header file. Then we define the add function in the add function we add two binary digits by passing value as arguments. In this function, we define a variable that is used on the addition of binary numbers. In this function we define the loop in the loop we check if two binary number is 1 then it will print 0 and pass 1 as the carry. after performing addition it will return value. In the main function, we take two binary numbers from the user and pass the value to the function and print function return value.

You might be interested in
Which item is essential to know before sketching a navigation menu flowchart?
alisha [4.7K]

Answer:

A, template specifics, such as horizontal or vertical menu layout

Explanation:

4 0
2 years ago
Read 2 more answers
When light does not pass through or bounce off an object, it is said to be
Dmitry [639]
The object is said to be absorbed
3 0
2 years ago
Read 2 more answers
How do you change the desktop background in the macos mojave operating system?
sleet_krkn [62]

Answer: Click the System Preferences icon on the dock. In the first row, click Desktop & Screen Saver.

Explanation: When it comes to macOS versions, Mojave and High Sierra are very comparable. The two have a lot in common, unlike Mojave and the more recent Catalina.

8 0
1 year ago
A type of employment test that is used to determine if personal and behavior preferences match the position.
aleksandrvk [35]

Explanation:

correct answer is D

Background check

8 0
2 years ago
Create a Python program to solve a simple pay calculation.
Travka [436]

Answer:

def weeklyPaid(hours_worked, wage):  

   if hours_worked > 40:  

       return 40 * wage + (hours_worked - 40) * wage * 1.5

   else:  

       return hours_worked * wage  

 

 

hours_worked = 50

wage = 100

 

pay = weeklyPaid(hours_worked, wage)  

 

print(f"Total gross pay: Rs.{pay:.2f} ")

Explanation:

provides gross pay

6 0
2 years ago
Other questions:
  • Which of the following is not an algorithm?
    8·1 answer
  • Joshua takes ownership of all his tasks. Which quality is he demonstrating?
    9·2 answers
  • The indent buttons on the home tab allow you to increase or decrease paragraph indenting in increments of ____ inches.
    5·1 answer
  • . Write a short program that asks the user to input a string, and then outputs the
    15·1 answer
  • If your internet were to go out, what steps would you take to troubleshoot to restore your service?
    15·1 answer
  • What tool can you use to discover vulnerabilities or dangerous misconfigurations on your systems and network
    5·1 answer
  • Which of these expressions is used to check whether num is equal to value?
    13·1 answer
  • What is file management?can please help me​
    14·2 answers
  • What does the word collaborative mean?
    13·2 answers
  • A. A set of electronic program that makes of computer perform tasks
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!