Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
As long as x is greater than 0
Output x % 2 (remainder is either 0 or 1)
x = x // 2
Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.
Ex: If the input is:
6
the output is:
110
Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's and 0's representing the integer in binary (in reverse). The function reverse_string() should return a string representing the input string in reverse.
def integer_to_reverse_binary(integer_value)
def reverse_string(input_string)
Note: This is a lab from a previous chapter that now requires the use of a function.

Answers

Answer 1

Answer:

#include <iostream>//header file

#include <string>//header file

using namespace std;

string integer_to_reverse_binary(int integer_value)//defining a method integer_to_reverse_binary  

{

   string ret = "";//defining string variable

   while (integer_value > 0) //using while loop to check integer_value value is greater than 0

   {

       ret += '0' + (integer_value % 2);//adding zeros in remainder value

       integer_value /= 2;//holding quotient value

   }

   return ret;//return string value

}

string reverse_string(string input_string)//defining a method reverse_string that holds a parameter user_String  

{

   string result;//defining a string variable  

   for (int i = 0; i < input_string.length(); ++i)//use for loop to calculate value  

   {

       result += input_string[input_string.length()-i-1];//add value in result variable

   }

   return result;//result result variable value

}

int main()//defining main method  

{

   int num;//defining integer variable

   string str;//defining string variable

   cin >> num;//input num value

   str = integer_to_reverse_binary(num);//use str variable to call the integer_to_reverse_binary method

   cout << reverse_string(str) << endl;//printing the reverse_string method value

   return 0;

}

Output:

6

110

Explanation:

In this code two string method "integer_to_reverse_binary and reverse_string" is defined that holds one parameter "integer_value and input_string".

In the first method a string variable is defined, that use the while loop to check integer value is greater than 0 and add zeros in the value and return its value as a string.

In the second it reverse the string value and store into the result variable, and in the main method the "num and str" variable is defined, and in the num it takes integer value and pass into the above method and print its return value.    


Related Questions

CH4 has how many of each type of atom?

Answers

Its easy that moderators that see this answer can think that my answer isn't without explanation.

• Type of atom C (Carbon)

C = 1

• Type of atom H (Hydrogen)

H = 4

You dont understand? JUST SEE THE FORMULA C MEANS ONLY HAVE 1 CARBON ATOM AND H4 MEANS 4 ATOM OF HYDROGEN

oK. have a nice day hope you understands

What is the influence of new technology on society?
Ο Α. .
New technology hardly has any impact on society.
OB.
New technology is only beneficial to society and cannot be detrimental.
OC.
New technology is detrimental, as it makes the existing technology obsolete.
OD. New technology normally utilizes a lot of resources and can adversely affect the economy.
O E.
New technology is beneficial but can also be usedly a detrimental way.

Answers

Answer:

E. New technology is beneficial but can also be used in a detrimental way.

Explanation:

New technology such as cryptocurrency (powered by blockchain technology) can be regarded as a welcome development that has benefited the society in so many good ways. However, cryptocurrency as a new technology also has disadvantages it presents to the society. One of such negative influence cryptocurrency has is that it can be used for illicit activities such as money laundering, funding terrorism and so on.

So, in summary, we can conclude that:

"New technology is beneficial but can also be used in a detrimental way."

Other Questions
Awswer the following metacognitive question using a maximum of three sentences. please help quick I will give brainliest :( My brain isn't working right now, please help. asexual reproduction involves two-parent organisms true or false 1: Read the dialogue below. Underline the simile.EMPEROR PENGUIN: (flaps wings as if in flight) Well, Parrot, as I can attest, notbeing able to fly isn't the end of the world. I must admit, however, that therest of its body is rather dull. (peers into clearing) That brown is as dull as dirt.Incline with literature can someone help me figure out which equation it is? Could someone solve and explain 10. Thanks! PLEASE HELP!!!question 1: What is the coefficient of the third term of the trinomial? (In other words, what is C?)(2x + 3)(4x 1) = 8x^2 +10x CQuestion 2: What is the coefficient of the second term of the polynomial? (In other words, what is B?)(x + 2)(2x^2 + x 1) = 2x^3 + Bx^2 + x 2question 3: What is the coefficient of the third term of the polynomial? (In other words, what is C?)(x + 2)(x^2 3x 2) = x^3 x^2 Cx 4 Please help meeeee!!! How many soldiers did General Santa Anna have? Thank you! Whats the solution to 2+7 First correct answer gets brainliest What was one common cause of crop failure in Georgia before World War II?lack of water due to droughtpoor soil due to overplantingfreezing due to extended wintersoverheating due to extended summers I TYPED IN THE ANSWER CAN SOMEONE CORRECT IF IT IS WRONG? PLZZZZZZZ I WILL MARK BRAINLIST1. Identify and explain a significant decision made by George Washington in his personal life that had immediate consequences for the United States of America.2. Identify and explain a significant decision made by George Washington in his personal life that continues to have consequences for the United States of America today.3. Identify and explain a significant decision made by George Washington as president that had immediate consequences for the United States of America.4. Identify and explain a significant decision made by George Washington as president that continues to have consequences for the United States of America today.5. Imagine that you are a merchant in Philadelphia, which served as the nations capital during most of Washingtons presidency. Identify and explain a decision you might make to turn a quick profit. Then, identify and explain a decision you might make to ensure long-term growth and stability for your business. Can someone plz help me with this one problem plzzzzz!!!! You are finding the SLOPE!! Please answer all of these! I will mark brainliest^^ 3+8Find the value of x WILL GIVE BRAINLIEST!Consider the following.twelve times the difference between a number and fifty-fiveTranslate into a variable expression. (Use x for your variable. Do not simplify.) Which statement correctly describes an interaction between organelles in the model?ALetter U and letter S control the collection and release of waste from the cell..Letter T and letter R store the genetic material necessary for replication of the cell.CLetter Q and letter S are responsible for creating and delivering nutrients throughout the cell.DLetter U and letter R are involved with the processes of transcription and translation for the cell.