Output not coming

//binary to decimal
#include
using namespace std;
int main()
{

int N;//number in binary format
cin>>N;
while(N>0)
{
	
	int binary=1;
    int ans=0;
    int last_digit=N%10;
	ans=ans+binary*last_digit;
	binary=binary*2;
	N=N/10;
	cout<<ans<<endl;
}
return 0;

}
INPUT:-101010
OUTPUT:-0
1
0
1
0
1

This ques is that you have to convert binary to decimal. For that you have to perform 3 changes in your code :

  1. int ans=0 should be before while loop.
  2. int binary=1 should also be before while loop.
  3. cout<<ans<<endl should be outside the while loop.
1 Like