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.

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++)
{
     if(j%2)
       cout << "1";
     else 
       cout << "0";
     j=j/2; // or for little optmized j=j>>2;
}

Edited 6 Years Ago by NicAx64: n/a

This question has already been answered. Start a new discussion instead.