Results 1 to 2 of 2
  1. #1
    fallout87 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Check Prime numbers program

    Hi, I'm working on a program that checks if a user entered integer is prime or not.

    It doesn't seem to work properly though. It thinks 25 is a prime number...and other ones that aren't or are prime is coming up wrong. Anyone have any ideas? Would be greatly appreciated.

    Cheers
    Fallout



    public static void checkPrimeNumber()
    {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter an Integer value: ");
    int n = sc.nextInt();

    int hF = (int) Math.sqrt(number);
    int prime = 0;

    for (int i = 2; i < hF; i++)
    {
    if (n % i == 0)
    {
    prime = 0;
    break;
    }
    else
    {
    prime = 1;
    break;
    }
    }
    if (prime == 1 || number == 2)
    {
    System.out.println(n + " is a prime number");
    }
    else
    {
    System.out.println(n + " is not a prime number");
    }
    programMenu();
    }
    Last edited by fallout87; 03-11-2012 at 07:42 PM. Reason: spelling & grammer

  2. #2
    skynet357 is offline Member
    Join Date
    Mar 2012
    Location
    Hyderabad, India
    Posts
    8
    Rep Power
    0

    Default Re: Check Prime numbers program

    Two changes
    ------------------------
    1. In the for loop use i<=hF.

    2. Instead of assigning prime=0 or prime=1 use prime++ for (hF%i==0 to be true).

    i mean

    if(hF%i==0){
    prime++;
    }

    no else part.

    3. if prime>0 then not prime. Else prime.


    I dint run these changes but I think this will work. :)

Similar Threads

  1. 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
  2. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 07:22 AM
  3. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  4. Computing prime numbers in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:39 PM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10: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
  •