Output is not coming correctly

//binary to decimal
#include
using namespace std;
int main()
{
int N;
cin>>N;
int n;
while(N>0)
{
cin>>n;
int binary=1;
int ans=0;
while (n>0)
{
int last_digit=n%10;
ans=ans+binarylast_digit;
binary=binary
2;
n=n/10;
}
cout<<ans<<endl;
N=N-1;
}

return 0;

}
Input:- 6
101010
Output:-42
0
0
0
0
0
why the output is like this?

@supratik260699 I suppose N variable is for the number of test cases. n is for the number in binary form. You are only giving one number ie. 101010 as input in binary form whereas the number of test cases you have considered here is 6. This is why for the remaining 5 cases, the output is 0.

1 Like