1 / 2
Jul 2020

please someone tell me what is wrong in my code

#include

using namespace std;

int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;

// Initializing base value to 1, i.e 2^0 
int base = 1; 

int temp = num; 
while (temp) { 
    int last_digit = temp % 10; 
    temp = temp / 10; 

    dec_value += last_digit * base; 

    base = base * 2; 
} 

return dec_value; 

}

int main() {
long long int t;
cin>>t;
while(t–){
long long int n;
cin>>n;
if(n&1){
cout<<n<<endl;
}
else{
int ans=0,p=1;
while(n>0){
int lb=(n&1);
ans=plb+ans;
p=p
10;
n=n>>1;
}
int rev_num = 0;
while(ans > 0)
{
rev_num = rev_num*10 + ans%10;
ans= ans/10;
}
cout<<binaryToDecimal(rev_num)<<endl;
}
}

return 0;

}

  • created

    Jul '20
  • last reply

    Jul '20
  • 1

    reply

  • 558

    views

  • 2

    users

  • 1

    link

Try this

1
10000000

btw, it’s a good idea to say which problem you’re trying to solve, or even better, link to it.
EC_CONB1

Also, please blockquote your code to prevent it from being interpreted. Either indent the whole lot by four spaces, or put three back ticks ``` on a line by themselves both before and after the code.