Can you please tell me what is wrong with this problem? one test fails | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can you please tell me what is wrong with this problem? one test fails

You need to know if a number is divisible by each of a group of other numbers. If you are given the number and the group of other numbers, you will test to make sure that it is divisible by all of them. Task: Test your number against all of the other numbers that you are given to make sure that it is divisible by them. Input Format: Two inputs: an integer value (the number you are testing) and a string of variable length of the integers that you should test against separated by spaces. Output Format: A string that says 'divisible by all' or 'not divisible by all'. Sample Input: 100 2 5 10 Sample Output: divisible by all https://code.sololearn.com/cYYdCiUsrn7h/?ref=app

7th Aug 2023, 5:13 PM
Андрей Колесник
Андрей Колесник - avatar
15 Answers
+ 5
I took a look at the code anyhow, and the only issue I found was that (other than not checking for invalid input) inside the loop it was doing an operation with modulus to check for divisibility before checking if the divisor was not zero. In that case your code could fail if you added 0 as one of the divisors. If you change that, it should work or at least bypass that logic error. Just for laughs, I rewrote it in a more functional style using the Streams API. Check it out if you like. I think it's more concise. https://www.sololearn.com/compiler-playground/c2ae4U0MA3Fw
7th Aug 2023, 6:55 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
+ 5
Андрей Колесник , there is an exception (dividion by zero) with these inputs: 7 0 8 2 0 this input is ok: 7 2 0
7th Aug 2023, 6:57 PM
Lothar
Lothar - avatar
+ 3
I rewrote your code with minor changes. It passed all the tests. You should know the features of the Scanner class. The variable num must be written as int num = sc.nextInt(); sc.nextLine(); or int num = Integer.parseInt (sc.nextLine().trim());
8th Aug 2023, 10:28 AM
Dzianis Mitrokhin
Dzianis Mitrokhin - avatar
+ 2
code of Marcos Chamosa Rodríguez is not openable so I'm reposting it https://code.sololearn.com/c2ae4U0MA3Fw/?ref=app
8th Aug 2023, 7:49 AM
zemiak
+ 1
It would be helpful if you specified which test fails.
7th Aug 2023, 6:00 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
+ 1
Marcos Chamosa Rodríguez, thank you very much for the answer) your code is awesome, but isn't working(
7th Aug 2023, 8:01 PM
Андрей Колесник
Андрей Колесник - avatar
+ 1
Lothar, thank you for your answer. I tested the code in Intellij Idea, and everything is working)
7th Aug 2023, 8:04 PM
Андрей Колесник
Андрей Колесник - avatar
+ 1
Dzianis Mitrokhin , thank you very much, it helps me) have a nice day, everyone!
8th Aug 2023, 11:43 AM
Андрей Колесник
Андрей Колесник - avatar
+ 1
@Marcos, in playground up-right is ikon for sharing and copy link. then link has same id code, but has different format and ends with ?ref=app
8th Aug 2023, 11:49 AM
zemiak
+ 1
I didn't find any wrong in given code. I checked this code in other platform it is working fine. Problem is in the taking input in sololearn playground.
8th Aug 2023, 5:38 PM
Deepak Kumar Prajapat
Deepak Kumar Prajapat - avatar
0
Unfortunately the test that fails is hidden... otherwise I could solve the problem
7th Aug 2023, 6:52 PM
Андрей Колесник
Андрей Колесник - avatar
0
That's too bad. It works just fine for me both in my PC and in the playground of solo learn. The compiler of the platform you're using likely is for a version of Java that is incompatible with some of my code. My guess is that it's Java 8. My code uses features from more recent versions.
7th Aug 2023, 8:07 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
0
playground uses Java 19 ea (early access), it means java 18 complete version https://code.sololearn.com/c1rk18PiBd95/?ref=app
8th Aug 2023, 7:27 AM
zemiak
0
Thanks zemiak. Where do you get those functional links for code? I can't find it
8th Aug 2023, 10:36 AM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
0
Ah, thank you!
8th Aug 2023, 12:02 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar