#include>
#include<math.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;int count=0;
for(int j=0;j<s.length();j++)
{
count = count+s[j]*pow(2,(s.length())-(j+1));
}
cout<<count<<endl;
}
}
im trying to do this problem in this way . please tell me why im not getting correct result.
Von newman loves binary
please help me sir in this
in the line where you are incrementing count
you have to do
count = count + ( s[j] - ‘0’ ) *pow(2,(s.length())-(j+1)) ;
this will convert the string/char value to a number that you can apply algebraic operations upon.
Please do the above change in your code. The rest of the logic is perfectly fine.
Hi Isha, since you have not been responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.