#what is wrong in this code

1 messages · Page 1 of 1 (latest)

last pilot
#

decimal to binary converter

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cin >> n;
int ans = 0;
int i = 0;
while (n != 0)
{
int bit = (n & 1);
ans = ans + (bit * pow(10, i)) ;
n = n>>1;
i++;
}

cout << ans;

return 0;

}

if i am using online compiler it is working correctly but if i use vs code then for digit 5 it is giving 100 instead of 101

worthy forge
#

;compile

turbid otterBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

worthy forge
#

;compile #include <iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cin >> n;
int ans = 0;
int i = 0;
while (n != 0)
{
int bit = (n & 1);
ans = ans + (bit * pow(10, i)) ;
n = n>>1;
i++;
}

cout << ans;

return 0;

}

turbid otterBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

worthy forge
#

;compile ```#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cin >> n;
int ans = 0;
int i = 0;
while (n != 0)
{
int bit = (n & 1);
ans = ans + (bit * pow(10, i)) ;
n = n>>1;
i++;
}

cout << ans;

return 0;

}

turbid otterBOT
#
Critical error:

You must provide a valid language or compiler!

;compile c++
```
int main() {}
```

worthy forge
#

;compile ```c++
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cin >> n;
int ans = 0;
int i = 0;
while (n != 0)
{
int bit = (n & 1);
ans = ans + (bit * pow(10, i)) ;
n = n>>1;
i++;
}

cout << ans;

return 0;

}

turbid otterBOT
#
Program Output
0
lunar galleon
#

you are converting decimal to binary but using 10 in the pow bracket. you should use 2.

last pilot
lunar galleon
#

I have never faced an issue while using gcc complier but sometimes vscode gives issues, so it might be because of that

last pilot
#

waise i got the answer. i just had to use float data type on ans rather than int

#

but still i don't know why it was giving error in int

lunar galleon
austere palm
#

rule 1 of programing

worthy forge
#

dont fix if not broken

austere portal
#

;compile

turbid otterBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

austere portal
#

;compile
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cin >> n;
int ans = 0;
int i = 0;
while (n != 0)
{
int bit = (n & 1);
ans = ans + (bit * pow(10, i)) ;
n = n>>1;
i++;
}

cout << ans;

return 0;

}

turbid otterBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

austere portal
#

;compile ```#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int n;
cin >> n;
int ans = 0;
int i = 0;
while (n != 0)
{
int bit = (n & 1);
ans = ans + (bit * pow(10, i)) ;
n = n>>1;
i++;
}

cout << ans;

return 0;

}```

turbid otterBOT
#
Critical error:

You must provide a valid language or compiler!

;compile c++
```
int main() {}
```

worthy forge
#

;compile

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int ans = 0;
    int i = 0;
    while (n != 0)
    {
        int bit = (n & 1);
        ans = ans + (bit * pow(10, i)) ;
        n = n>>1;
        i++;
    }

    cout << ans;

    return 0;
}
turbid otterBOT
#
Program Output
1