I am new to programming and I've been practising C++ in VS Code with Coderunner and Microsoft C/C++ extensions enabled. I wrote this code to convert decimal numbers to binary numbers.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int d;
int c = 0;
cout << "Enter a decimal number : ";
cin >> d;
for(int i=0; d!=0; i++)
{
c += pow(10,i)*(d%2);
d = d/2;
}
cout << "Binary value : " << c;
}
But it is giving me wrong results. For e.g. upon giving 5 as input the output is 100 while it should be 101. I've also run this same code on other compilers including online compilers and it's working fine. Also by replacing int c with float c it is working fine in VS Code too. But I don't understand why it is not working with int c as the logic is correct. If anyone can explain me I would be really thankful.