How do I complete this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I complete this?

What do I put after the return? If I put y, if gives me zero. I can't put b as I understand it. Or is this code incomplete? https://code.sololearn.com/cjz1jlR561ho/?ref=app

19th Dec 2020, 5:16 PM
Mayur Gowda
Mayur Gowda - avatar
4 Answers
+ 7
Mayur Gowda You need to return String. Every time you are reinitialising b so how you can get actual result. You need to declare b outside the loop. public static String toBinary(int y) { String b=""; while(y>0) { b=(y%2)+b; y/=2; } return b; }
19th Dec 2020, 5:48 PM
A͢J
A͢J - avatar
+ 2
it wouldn't if you change the return type of the method too. public static String toBinary(int y)
19th Dec 2020, 6:01 PM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
Right... Thanks Bahha🐧. I didn't realize that I Am Groot ! had written the same thing as well. Thanks to both of you. Much appreciated for the help.
19th Dec 2020, 6:03 PM
Mayur Gowda
Mayur Gowda - avatar
0
I Am Groot !, But doesn't that give an error saying, string cannot be converted to int.
19th Dec 2020, 5:51 PM
Mayur Gowda
Mayur Gowda - avatar