How do I reverse the output of a program?

DEdesigns57 picture DEdesigns57 · Oct 20, 2011 · Viewed 7.1k times · Source

I have to convert decimal numbers like 43.62 to binary. So i first wrote a basic program that converts 43 into binary. But I notice that my program prints out the binary number in reverse, so it prints 1 1 0 1 0 1 instead of 1 0 1 0 1 1. how can I fix this.

My Code:

#include <iostream>

using namespace std;

int main()
{
    int number;
    int remainder;

    cout << "Enter a integer: ";
    cin >> number;

    while(number != 0)
    {
        remainder = number % 2;
        cout << remainder << " ";
        number /= 2;
    }

    int pause;
    cin >> pause;

    return 0;
}

Answer

ObscureRobot picture ObscureRobot · Oct 20, 2011

Instead of sending each digit to cout, send them to an array. Then read the array out in reverse order. Or push them onto a stack, and then pop them back off the stack. Or...