DEV Community

Madhuban Khatri
Madhuban Khatri

Posted on

Decimal to Binary conversion in C++

You can access this code on my Github profile.

#include<iostream>
using namespace std;

int main(){
    int n;
    cout<<"Enter a Number: ";
    cin>>n;
    int rem=n%2;
    while(true){

        if(rem==0){
            n=n/2;
            int rem=n%2;
            cout<<rem;
        }
        else{
            int rem=n%2;
            n=n/2;
            cout<<rem;
        }

        if(n==0){
            break;
        }
    }
}```

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

This is inefficient due to division and modulus. You really should cast to uint64_t and use << and &.