Welcome to Dream.In.Code
Become a Java Expert!

Join 150,381 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,470 people online right now. Registration is fast and FREE... Join Now!




Prime number checker

 
Reply to this topicStart new topic

Prime number checker

Thenextneo
24 Jan, 2008 - 04:22 PM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 23


My Contributions
Supposed to write a program using methods that utilizes a checker for prime numbers and then returns a true or false statement. I belive I have screwed this program up bad but im on the right side of the ball at least.

CODE
/**
* DJ Hill
* Computer Science Ap Online course
* Gary Langner
* OS Vista / Programmer BlueJ
*/
import java.util.*;
import java.text.*;

public class primenumber
{
public static boolean isPrime(int n)
{        boolean prime = true;
        for (int i = 3; i <= Math.sqrt(n); i += 2)
        if (n % i == 0)
        {    
            prime = false;
            break;
          }
          if (( n%2 !=0 && prime && n > 2) || n == 2)
          {            
              return true;        
          }
          else
          {            
              return false;
          }    
  
}
public static void main (String[] args)
{
Scanner in=new Scanner(System.in);

System.out.println("Please input a number you wish to test if it is prime.");
int n=in.nextInt();
isPrime(int n);

}
}


What I am having problems with at the moment is when you try to initialize the compile it will say that isPrime(int n); does not have a .class and needs something, and I dont knwo what.
User is offlineProfile CardPM
+Quote Post

Thenextneo
RE: Prime Number Checker
24 Jan, 2008 - 04:39 PM
Post #2

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 23


My Contributions
Alright, I found out how to fix the part about it compiling. Just changed the line

isPrime(int n); to isPrime(n); but now it does not print the result true/false
User is offlineProfile CardPM
+Quote Post

Todilo
RE: Prime Number Checker
25 Jan, 2008 - 12:28 AM
Post #3

New D.I.C Head
*

Joined: 13 Nov, 2007
Posts: 34


My Contributions
Ok here is a fix.

CODE

/**
* DJ Hill
* Computer Science Ap Online course
* Gary Langner
* OS Vista / Programmer BlueJ
*/
import java.util.*;
import java.text.*;

public class primenumber
{
public static boolean isPrime(int n)
{        boolean prime = true;
        for (int i = 3; i <= Math.sqrt(n); i += 2)
        if (n % i == 0)
        {    
            prime = false;
            break;
          }
          if (( n%2 !=0 && prime && n > 2) || n == 2)
          {            
              return true;        
          }
          else
          {            
              return false;
          }    
  
}
public static void main (String[] args)
{
Scanner in=new Scanner(System.in);

System.out.println("Please input a number you wish to test if it is prime.");
int n;
while((n=in.nextInt())!=-1) {

System.out.println(isPrime(n));
}


}
}


The changes are in the main method. So first it asks for a number. and then it assigns this number to n(in the while) and as long as that value differs from -1 you can try a new number. The system.out.println prints the result of the returned value from isPrime(n), that is either true or false. Hope this helps
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Prime Number Checker
26 Jan, 2008 - 12:48 AM
Post #4

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions
change the return lines 2 :
prime = (true/false);

add a return statement at the end of the loop tht says:
return prime;

change the last line to
System.out.println(isPrime(n));

u can add the while loop in as well like Todilo did
or keep it so it only asks once it dnt matter

writing it like this makes ur result more accessable if u later decide to write more methods (because its stored in a variable other classes will be able to access it)

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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