Binary Conversion

I wrote the code and it passes almost all the cases i put manually except for a number other than 0 or 1, it just gives 0 if tested for 4 or 5 or any number. But when I submit the code both the test cases fail. Please help me.
import java.util.Scanner;

public class Binary {

public static void main(String[] args) {
	Scanner scn = new Scanner(System.in);
	int N = scn.nextInt();
	
	int base = 1;
	int temp = N;
	int value = 0;
	
	while(temp > 0) {
		
		int rem = temp%10;
		temp = temp/10;
		if(rem == 1 || rem == 0) {
		value += rem*base;
		base = base * 2;
		
		}
	}
	System.out.println(value);
}

}

Hi…
Your logic is absolutely correct.
the problem you are facing is you are not taking testcases.
After taking testcases,There wont be any problem left.
Hit like,if this solves your problem.

When I put my code in it gives all the answers, I even downoaded the test cases and tried each and every case inside it everything was passing. Earlier, I was getting 0 as output when put a number 5 or 7 but in the test cases these inputs got no output so I fixed it that way. Now, the tester says- Test Case 1 : No output and Test Case 2: Wrong Answer. Please help, I don’t want to be stuck on this one any longer. My revised code: Scanner scn = new Scanner(System.in); int N = scn.nextInt(); int base = 1; int temp = N; int value = 0; while(temp > 0) { int rem = temp%10; temp = temp/10; if(rem == 1 || rem == 0) { value += rem*base; base = base * 2; } } if(N%10 == 1 || N%10 == 0) { System.out.println(value); }

Hi I again said your logic is absolutely correct.The main problem is you were not taking test cases into consuderation.
I modified it for you.
Hit like if it solves your doubt.
And mark doubt as resolved too.
Thankyou,

public class Binary {

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int tc=scn.nextInt();
while(tc–>0){

int N = scn.nextInt();

int base = 1;
int temp = N;
int value = 0;

while(temp > 0) {

  int rem = temp%10;
  temp = temp/10;
  if(rem == 1 || rem == 0) {
  value += rem*base;
  base = base * 2;
  
  }

}
System.out.println(value);
}
}
}

This doesn’t seem fine at all. Firstly you introduced two inputs from the user where the first input is for tc and second for N. And putting this code up on the answer gave run time error for both the test cases.
Could you please explain on this a bit.

Hi I ran this code and got all test cases passed.
I think you had done brackets mismatch during pasting the code.

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.