Sample cases running fine but test cases are failing.
Test cases are failing
@Jan19LP0018 even the sample test case is failing by your code :
sample test case :
101010
correct output : 42
your output : 11000101010010010
Your doesnt have to print binary value your have to print the decimal base value.
Correct code :
public static 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 > 0) {
int last_digit = temp % 10; // Extract rightmost digit
temp = temp / 10; // Update the number
dec_value += last_digit * base; // Add the digit to the answer
base = base * 2; //Update the power of 2
}
return dec_value; // Return ans
}
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.