So I have written a code for printing first 20 Binary Numbers like 0 1 10 11 100 101 and so on...
I Tried running the code in Atom Editor it's not accurate but when I ran the same code in an Online compiler it gave me a correct answer which I expected
This is the code that I used :
#include<iostream>
#include<math.h>
using namespace std;
int toBinary(int num){
int ans = 0;
int i = 0;
while(num!=0){
int bit = num&1;
ans = ( bit * pow(10,i) ) + ans;
num = num>>1;
i++;
}
return ans;
}
int main(){
for(int i=0;i<20;i++){
cout<<toBinary(i)<<endl;
}
return 0;
}
This is the output i'm getting in ATOM editor:
0 1 10 11 99 100 109 110 1000 1001 1010 1011 1099 1100 1109 1110 9999 10000 10009 10010
And this is the output I'm getting in Online Compiler (this is the output I expect):
0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010 10011