Von Neuman Loves Binary

Why these 2 are giving different output:

int num=0,a;
cin>>a;
for(int i=0;a>0;i++)
{
num=num+((a%10)*pow(2,i));
a=a/10;
}
cout<<num;

int num=0,a;
a=010;
for(int i=0;a>0;i++)
{
num=num+((a%10)*pow(2,i));
a=a/10;
}
cout<<num;

for a=010???

@anonymous43 you second part of code in which you have taken a=010 donot give the same answer as compiler will take a in octal form that is octal form of 010 is 8 ie 1*8^0 therefore it is taking a as 8 therefore not giving same answer.You can compile second part and can print value of a after initialising and see the result. Hope you get it

Hey @anonymous43
If you write
int a = 010;
010 is a three digit number starting with 0.
Actually, any three digit number starting with 0 as an octal number, so compiler will treat 010 as 8
You may try writing 10 here instead of 010, that’d give the right ans.