Results 1 to 3 of 3
  1. #1
    xswtnsour13x is offline Member
    Join Date
    Sep 2013
    Posts
    2
    Rep Power
    0

    Default Java program prime or not a prime number

    I need to write a program to:

    1. ask the user to enter a positive integer

    2. check if the number is a prime number or not. If the number is a prime number, display a message like "59 is a prime number" where 59 is the number user entered. Otherwise, display the message like "57 is not a prime number" where 57 is the number user entered.

    3. ask the user if he/she wants to check another number and wait for the user to enter "Y" or "y" as yes, "n" or "N" as no. If yes, repeat the three steps. If no, exit the program. For all other letters, reject ti and ask the user to enter only "y" or "n".

    It's been driving me crazy for hours. I don't know where to start at all. Or how to start. I learn better by reading but this is not in the book. I just can't learn when the instructor is giving a lecture. I am in serious need for help on this especially because it is due in a few hours ( it was assigned today in the afternoon and due tonight). It usually takes days for me to solve a problem but since this is due the same day, I am really stressed out. Please help.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java program prime or not a prime number

    Dismantle the problem: for now forget about all that IO hoopla and concentrate on the core: what makes a number a prime number? Wouldn't it be nice to have a boolean method isPrime(int n) that returns true only if n is a prime number. Design an implement that method first:

    Java Code:
    public boolean isPrime(int n) {
       // here goes your code ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Java program prime or not a prime number

    From point 2 I can imagine someone doing this:
    Java Code:
    if(isPrime(num)) {
        System.out.println("like \"59 is a prime number\" where 59 is the number user entered");
    } else {
        System.out.println("like \"57 is not a prime number\" where 57 is the number user entered.");
    }

Similar Threads

  1. Find a prime number
    By bam207 in forum New To Java
    Replies: 2
    Last Post: 11-16-2012, 06:42 PM
  2. Prime Number finder
    By hoosierfan24 in forum New To Java
    Replies: 11
    Last Post: 02-17-2011, 09:13 AM
  3. displaying prime number
    By eng_hyzoom in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 11:21 AM
  4. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 11:42 PM
  5. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 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
  •