232k views
2 votes
In c++, make the output exactly as shown in the example.

In c++, make the output exactly as shown in the example.-example-1

1 Answer

3 votes

Answer:

Here's a C++ program that takes a positive integer as input, and outputs a string of 1's and 0's representing the integer in reverse binary:

#include <iostream>

#include <string>

std::string reverse_binary(int x) {

std::string result = "";

while (x > 0) {

result += std::to_string(x % 2);

x /= 2;

}

return result;

}

int main() {

int x;

std::cin >> x;

std::cout << reverse_binary(x) << std::endl;

return 0;

}

The reverse_binary function takes an integer x as input, and returns a string of 1's and 0's representing x in reverse binary. The function uses a while loop to repeatedly divide x by 2 and append the remainder (either 0 or 1) to the result string. Once x is zero, the function returns the result string.

In the main function, we simply read in an integer from std::cin, call reverse_binary to get the reverse binary representation as a string, and then output the string to std::cout.

For example, if the user inputs 6, the output will be "011".

Hope this helps!

User Millikan
by
7.8k points

No related questions found