View Single Post
Old 10-31-2006, 08:22 AM   #5 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
My code works fine for c++. You just need to include library stdio.h.

Code:
#include <stdio.h>
#include <iostream>
using namespace std;

void printBin( int var ) {
  int i;

  for (i=11; i>=0; i--)
    printf("%d", (var >> i) & 1);
  putchar('\n');
}

int main() {
  int n;  // number to convert to binary

    while (cin >> n) {
        if (n > 0) {
            cout << n << " (decimal) = ";
            printBin(n);
            cout << " (binary) in reverse order" << endl;
        } else {
            cout << "Please enter a number greater than zero." << endl;
        }
    }
  return 0;
}
__________________
toe_cutter is offline   Reply With Quote