I'm Getting wrong answer ,although my code is correct

#include
#include<math.h>
using namespace std;
int main()
{
int n;
int sum=0;

while(n!=0)
{	int digit=0;
	int rem=n%10;
	sum=sum+rem*pow(2,digit);
	digit++;
	n=n/10;
}
cout<<sum<<endl;
return 0;

}

int digit=0 should come outside the while loop
otherwise it will initialize with 0 in each iteration

Still wrong :frowning:
Input: 101010
Expected output:42
My output: 104

Thank you !!
cin>>n was missing !!

please take input :rofl:

Modified Code

i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like