JAVA: My code works, but gives one result for all test cases. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JAVA: My code works, but gives one result for all test cases.

EDIT: SOLVED This code counts how many numbers are there, whose digits summation is 19 or 19-multiple from 1 to the given input number, n. The program will then print how many numbers there are satisfying the condition mentioned above. n must be between 100 and 1000000 inclusive. So if the input is 200, the output must be 1. From 1 to 200, there is only one number, whose digits add up to 19 or 19's multiple. This number is 199. This is because 1+9+9 = 19 My code only outputs the count as 0. I'm trying to find my mistake, but help is appreciated. https://code.sololearn.com/cl4ZQ7QZM6W3

5th Dec 2021, 12:41 PM
Salma D. Elabsi
Salma D. Elabsi - avatar
2 Answers
+ 3
Salma D. Elabsi What if sum is 114? Do this instead of comparing with Hard Code value: if(sum % 19 == 0) You are not using i. You should assign i to a new variable then use that variable in while loop. https://code.sololearn.com/cF9c68wUfq2z/?ref=app
5th Dec 2021, 2:48 PM
A͢J
A͢J - avatar
+ 4
In your code you are manipulating n. It gets 0 in your while loop so your for loop runs only one times. Before you start your while loop: int num = i; //Don't manipulate i because your for loop is working with it sum = 0; //reset sum Then your while loop where you just change n to num.
5th Dec 2021, 1:42 PM
Denise Roßberg
Denise Roßberg - avatar