int n,s=0;
cin>>n;
while(n>0)
{
s+=(n%10);
s=s<<1;
n=n/10;
}
cout<<s;
/* Above code giving wrong output for numbers which ends with 0 ,how do i correct it?*/
Binary to decimal conversion
@aniket07
You would require the help of a third variable to solve this problem which will act as a multiplier factor for s.
int n, s = 0, b = 1;
cin>>n;
while (n > 0) {
s += (n % 10) * b;
b = b << 1;
n = n / 10;
}
cout<<s;
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.