Von neuman loves binary

#include
using namespace std;

int main() {
int r,i, n,N,ans=0,p=1;
cin>>n;

	for(i=0;i<n;i++)
	{
		cin>>N;
       while(N>0)
		{r=N%10;
		ans=ans+r*p;
		p=2*p;
		N=N/10;

}

		cout<<ans<<endl;

	}
  

return 0;

}
i am note getting the correct answer

@utkarsh2504
hello Utkarsh,
a) use long long datatype
b) declare and intialise ur ans and p variable inside for loop.

1 Like

Compiling failed with exitcode 1, compiler output: prog.cpp: In function β€˜int main()’: prog.cpp:13:4: error: expected β€˜,’ or β€˜;’ before β€˜while’ while(N>0) ^~~~~ prog.cpp:5:7: warning: unused variable β€˜r’ [-Wunused-variable] int r,i,N; ^ prog.cpp:5:11: warning: unused variable β€˜N’ [-Wunused-variable] int r,i,N; ^

i got this error after doing that

pls save ur code on-> https://ide.codingblocks.com/ and share the generated link


check this

a) changed datatype to long long
b) reading N outside while loop

1 Like

well it is running correctly on code blocks but here i am still getting errors ans varibale not declared in correct scopes

it is working fine in coding blocks ide as well ,pls try again

The Right of Von Neuman Loves Binary

#include
using namespace std;
int main()
{
int no;
int N;
cin>>N;
while(N>0) {
cin>>no;
int ans=0;
int p=1;

    while(no>0)
           {
             int r=no%10;
             ans =ans + r*p;
             p=p*2;
             no=no/10;
            }
     cout<<ans<<endl;
     N=N-1;
    }

return 0;
}