Results 1 to 8 of 8

Thread: Prime numbers

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

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Prime numbers

    Do you have a question?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Prime numbers

    Is it correct ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Prime numbers

    What happens when the code executes? Does it produce the desired results?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Prime numbers

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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Prime numbers

    Is that the desired output?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Prime numbers

    Quote Originally Posted by shad0wblade890 View Post
    im not sure on the last step tho
    What does that mean? It appears to be working.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. prime numbers
    By Daniel93 in forum New To Java
    Replies: 8
    Last Post: 01-06-2013, 08:33 PM
  2. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM
  3. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 08:22 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 07:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:09 AM

Posting Permissions

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