I need help to complete the task. | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

I need help to complete the task.

Task: While Loops A client wants you to write a program that prints all numbers from 1 to the inputted number that are either a multiplier of 3 or end with 3. Sample input 14 Sample output 3 6 9 12 13 My code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int number = read.nextInt(); for(int x=3; x<=number; x=x+3) { System.out.println(x); } } } What can I do?

19th Aug 2021, 5:31 PM
Lisa Kraack
Lisa Kraack - avatar
2 Respostas
+ 5
Or you can do it like this for(int x=1; x<=number; x++) { if(x%3 == 0 || x%10 ==3){ System.out.println(x); } }
20th Aug 2021, 2:02 AM
Simba
Simba - avatar
+ 4
A common way to check divisibility is if x % 3 == 0 In order to check whether the number end with 3, you could convert the number to a string and then check if the last letter equals 3. (Please put your code intoba playground script and link it)
19th Aug 2021, 5:37 PM
Lisa
Lisa - avatar