Decimal to Binary program not working on my local machine but works perfectly on online compiler

57 Views Asked by At

I was writing a C++ program to convert decimal to binary, but my local system is giving different and wrong output, but when i run same program on programiz c++ online compiler it gives desired output.

#include<iostream>
#include<stdlib.h>
#include<math.h>

using namespace std;

int main(){
    // system("CLS");
    cout<<"DECIMAL TO BINARY"<<endl;
    int n;
    cin>>n;
    int ans=0;
    int i=0;
    while(n){
        // extracting last bit
        // if last bit is 0 then 0&1->0
        // if last bit is 1 then 1&1->1
        int last_bit=(n&1);
        ans=(last_bit*pow(10, i))+ans;
        i++;
        // removing last bit
        n>>=1;
    }
    cout<<ans;
}

is it the logic of above code wrong or compiler dependent?

The output on my local machine not getting binary output The Output on programiz c++ online compiler getting binary output

0

There are 0 best solutions below