- 2
Please help me....
Please help me solve it A client wants you to write a program that prints all numbers from 0 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 attempt import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int x = read.nextInt(); //your code goes here while(x%3==0 || x%10==3) { System .out.println(x); x++; } } }
7 Answers
0
You are starting from x=input number to infinity..
You shloud start from 0 to towards x.
Use a for loop(to be clear and not to confuse), from i=0 to x, do i%3==0 || i%10==3... Until i<x
Edit :
muhammed sinan
in your code, if your input is 10 then output is :
12
13
15
18
....
.... (infinite loop)...
0
Use another variable to set while condition to it or use for loop as up Jayakrishnađźđł mentioned.
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
//your code goes here
int count =1;
while(count<=number){
if((count%3)==0||(count%10)==3){
System.out.println(count);
}
count++;
}
}
}
or you can use if statement regarding your answer, muhammed sinan
0
this way you can do that
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
int i = 1;
//your code goes here
while(i<=number){
if (i%3==0|| i%10==3){
System.out.println(i);
}
i++;
}
}
}
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
int z = 0;
//your code goes here
while (number > z) {
z ++;
if (!(z % 3 == 0) && (!(z % 10 == 3))) {
continue;
}
System.out.println(z);
}
}
}
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
int f = 1;
//please Subscribe to My Channel
//name: Fazal Tuts4U
while(f <= number)
{
if(f%3==0 || f%10==3)
{
System.out.println(f);
}
f++;
}
}
}
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int number = read.nextInt();
//your code goes here
int count=1;
while(count<=number){
if((count%10==3)||(count%3==0)){
System.out.println(count);
}
count++;
}
}
}
**This is correct answer