1. Member
Join Date
Nov 2013
Posts
29
Rep Power
0

## Prime numbers

print prime numbers up to a given maximum value.
Accept the maximum value as a user input.
A prime number is only divisible by 1 and itself.

Java Code:
```package prime;

import java.util.Scanner;

public class prime {

public static void main(String[] args) {
//----------------------------------
//User input
//----------------------------------
Scanner userinput = new Scanner (System.in);
System.out.println("enter a number");
int input = userinput.nextInt();
//----------------------------------
//The for/if loop
//----------------------------------

for (int l=2; l<=input; l++)
{
for (int k=2; k<=l; k++)
{
if (l==k)
{
System.out.println(l);
}
if (l%k==0)
{
break;
}
}
}
}
}```

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,502
Rep Power
9

## Re: Prime numbers

Do you have a question?

Regards,
Jim

3. Member
Join Date
Nov 2013
Posts
29
Rep Power
0

## Re: Prime numbers

Is it correct ?

4. ## Re: Prime numbers

What happens when the code executes? Does it produce the desired results?

5. Member
Join Date
Nov 2013
Posts
29
Rep Power
0

## Re: Prime numbers

the results are :
enter a number
10
2
3
5
7

6. ## Re: Prime numbers

Is that the desired output?

7. Member
Join Date
Nov 2013
Posts
29
Rep Power
0

## Re: Prime numbers

the steps are:
print prime numbers up to a given maximum value.
Accept the maximum value as a user input.
A prime number is only divisible by 1 and itself.

so im guessing its right output and coding... im not sure on the last step tho

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,502
Rep Power
9

## Re: Prime numbers

im not sure on the last step tho
What does that mean? It appears to be working.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•