Thread: C++ Help
View Single Post
Old 11-08-2008   #7
Google

 
Google's Avatar
 
Last Online: 05-30-2013
Join Date: Jan 2008
Posts: 1,788
Thanks: 10,018
Thanked 1,100 Times in 651 Posts
Groans: 1
Groaned at 6 Times in 6 Posts
Default Re: C++ Help

Decimal to Binary Converter:

Code:
#include <iostream.h>
void binary(int);
void main() {
    int number;
    cout << "Enter a positive integer: ";
    cin >> number;
    if (number < 0) 
        cout << "The number must be positive you idiot!\n";
    else {
        cout << number << " is ";
        binary(number);
        cout<<" in binary.\n\n";
    }
}
void binary(int n) {
    int rem;
    if(n <= 1) {
        cout << n;
        return;
    }
    rem = n%2;
    binary(n >> 1);    
    cout << rem;
}
Google is offline   Reply With Quote