- Joined
- Jun 15, 2003
- Location
- Cedar Rapids, Iowa
Assignment is to write a program to find the prime numbers that are less than a number you enter. Here's what I have now:
Here's my output (when I enter 15 as upto):
I know that I can make it more efficient, but I wanted to get it working in an easier-to-understand way first.
Code:
public class Lab6Part3 {
public static void main (String[] args) {
int x, y, upto;
boolean prime;
Scanner kb = new Scanner(System.in);
System.out.print("\nEnter a number to find all prime numbers up to it: ");
upto = kb.nextInt();
for (x=2; x<upto; x++) {
prime=false;
for (y=2; y<upto; y++) {
if (x%y!=0)
prime=true;
else
prime=false;
}
if (prime=true)
System.out.print(x + " ");
}
}
}
Here's my output (when I enter 15 as upto):
Code:
Enter a number to find all prime numbers up to it: 15
2 3 4 5 6 7 8 9 10 11 12 13 14
I know that I can make it more efficient, but I wanted to get it working in an easier-to-understand way first.