Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,421 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,338 people online right now. Registration is fast and FREE... Join Now!




Prime Number Issue

 
Reply to this topicStart new topic

Prime Number Issue, Please advise

safialan
14 Oct, 2008 - 09:00 PM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 7

Hi there,

Can anyone please help me with this issue???

I need to write an application that will prompt the user for a number and determine whether or not it is prime.

Application should ask the user to enter a positive integer greater than 2 or a -1 to quit.

If it is prime, it will output a message saying "x is a prime number."

If the number is not prime it will output a message saying " x is not a prime number." It will then output the factors for that number. Ex: 18 is not a prime number. Its factors are: 1,2,3,6,9,18

I came up to the part to create the loop, but I don't know where to start. I want to have a loop running from 2 to the square root of x. I will assume that the number entered is prime and will divide it by mod 2, if it's divisible than it'll say the number is not prime.

Here is my code, it's not working though... Please advise me if my approach is incorrect. Many thanks in advance...

CODE


import java.util.Scanner;  

public class PrimeNumbers {
    
public static void main (String[] args){  
    
      
        Scanner input = new Scanner( System.in );  
        
       int x;
       int integer;
       int integerCounter;
      
      
      
       integerCounter = 0;
       x= 0;
      
    
      
        
       System.out.println ("Please enter a positive integer greater than 2 or a -1 to quit");
       integer = input.nextInt();
      
       while (integer != -1)
       {
           integerCounter = integerCounter + 1;
           System.out.println ("Please enter a positive integer greater than 2 or a -1 to quit");
           integer = input.nextInt();
           
       }
         for (int i = 2;Math.sqrt(i); i++){
             
             if (x % mod(2) == 0)
             
             System.out.println (integer +"is not a prime number");
             else {
                 System.out.println (integer + "is a prime number"); }
         }
        
      
        }
        }
        






User is offlineProfile CardPM
+Quote Post

pbl
RE: Prime Number Issue
14 Oct, 2008 - 09:06 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
you sure your

if (x % mod(2) == 0)

shouldn't be

if (x % i == 0)

you are just testing sqrt(i) times that your number is a multiple of 2 or not if mod(2) is a valid syntax

This post has been edited by pbl: 14 Oct, 2008 - 09:07 PM
User is offlineProfile CardPM
+Quote Post

safialan
RE: Prime Number Issue
15 Oct, 2008 - 06:53 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 7

I corrected it that way, but it still doesn't work sad.gif I am about to give up...
JCreator gives the error "incompatible types"....

This post has been edited by safialan: 15 Oct, 2008 - 07:12 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Prime Number Issue
15 Oct, 2008 - 07:59 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
sqrt(of a int) may need a +1
CODE

        Scanner input = new Scanner( System.in );  

        for(;;) {
            
            System.out.println ("Please enter a positive integer greater than 2 or a -1 to quit");
            int integer = input.nextInt();
            if(integer == -1)
                break;
            boolean isPrime = true;
            for (int i = 2;  i < (int) Math.sqrt(integer) + 1; i++) {

                if (integer % i == 0) {
                    System.out.println (integer + " is not a prime number");
                    isPrime = false;
                    break;
                }
            }
            if(isPrime)
                System.out.println (integer + " is a prime number"); }

    }  

User is offlineProfile CardPM
+Quote Post

safialan
RE: Prime Number Issue
15 Oct, 2008 - 08:06 PM
Post #5

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 7

Thank you very much pbl... You are a GENIUS!!! It is working now woo hooo, I cannot believe my eyes smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:12PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month