Question

4.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...

4.14 LAB: Convert to binary

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.

Ex: If the input is:

6

the output is:

011

#include <iostream>
using namespace std;

int main() {

   /* Type your code here. */

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
#include 
#include 

using namespace std;

int main() {
    int n;

    cin >> n;

    while (n > 0) {
        cout << n % 2;
        n /= 2;
    }

    cout << endl;   // remove this line if you want to remove newline character at the end
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
4.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT