+ 1

Why does it ask for a return statement? I think I have one... Help please!

import java.util.Scanner; //your code goes here public class Converter{ static String toBinary(int num){ String binary=""; while(num > 0) { binary = (num%2)+binary; num /= 2; return (binary); } } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } }

21st Jul 2021, 4:38 PM
Paul Berger
Paul Berger - avatar
2 Answers
+ 4
Paul Berger It is giving error because it is not necessary to have num > 0 so return statement will not execute because it is written inside while loop. So write return statement outside the loop.
21st Jul 2021, 5:16 PM
A͢J
A͢J - avatar
+ 2
thanks alot!
21st Jul 2021, 6:54 PM
Paul Berger
Paul Berger - avatar