i am to write a C++ program to output the binary (base – 2) representation of a decimal integer. the program should accept a positive integer from the user. After verifying that the input is valid, the program should call a function named toBin, which outputs the binary representation of the number.

The function toBin must not be a recursive function.

I am a beginner in C++.I don't know where to start and don't really understand the question. It would be really helpful if someone could show me an example of what i am supposed to do. Thank you.

7 Years
Discussion Span
Last Post by NicAx64

you can use the '>>' operator or divide by 2 (same thing).
But '>>' operator is really speed then the divide by 2(but in
modern day computing it doesn't count a lot).

for a example you got a integer 'j' to convert to binary.
Then, this is the algorithm.

int j=1234567;
int i;
for (i=0;i<(sizeof(int)*8);i++)
       cout << "1";
       cout << "0";
     j=j/2; // or for little optmized j=j>>2;

Edited by NicAx64: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.