468,167 Members | 1,936 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,167 developers. It's quick & easy.

How do I write a method named isPrime, which takes an integer as an argument and

A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6.
Write a method named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the method in a complete program.

This is what I have so far.

import javax.swing.JOptionPane;

public class PrimeChecker
{

public static void main(String[] args)
{
String input; // To hold keyboard input
String message; // Message to display
int number; // Number to check for prime

// Get the number.
input = JOptionPane.showInputDialog("Enter a number.");
number = Integer.parseInt(input);

// Determine whether it is prime or not.
if (isPrime(number))
message = "That is a prime number.";
else
message = "That is not a prime number.";

// Display a message.
JOptionPane.showMessageDialog(null, message);

System.exit(0);
}

}
Mar 26 '10 #1
5 8713
jkmyoung
2,057 Expert 2GB
Alright, where is the code for your isPrime function? What algorithm do you intend to use?
Mar 26 '10 #2
I want to use the % operator, but I don't know how to use it. I also want to use the code String num = but I don't know what to put after it. I went and added so stuff but I don't know if it is correct. This is what I have now.

import javax.swing.JOptionPane;

public class PrimeChecker
{
String num =

if (number % number == 0 || number % number == 1)
status = true;
else
status = false;



public static void main(String[] args)
{
String input; // To hold keyboard input
String message; // Message to display
int number; // Number to check for prime

// Get the number.
input = JOptionPane.showInputDialog("Enter a number.");
number = Integer.parseInt(input);

// Determine whether it is prime or not.
if (isPrime(number))
message = "That is a prime number.";
else
message = "That is not a prime number.";

// Display a message.
JOptionPane.showMessageDialog(null, message);

System.exit(0);
}

}
Mar 27 '10 #3
pbrockway2
151 Expert 100+
I want to use the % operator, but I don't know how to use it. I also want to use the code String num = but I don't know what to put after it.
The remainder operator (%) is used like any other binary arithmetic operator: you put it in between two values to obtain a third.

Expand|Select|Wrap|Line Numbers
  1. int result = 6 * 7;
  2. System.out.println(result); // prints 42
  3. result = 5 % 2;
  4. System.out.println(result); // prints 1
  5.  
As for initialising a string variable, you can use a string literal (something in "quotes") or any expression that yields a string:

Expand|Select|Wrap|Line Numbers
  1. String num = "Hello world";
  2. System.out.println(num); // prints Hello world
  3. num = String.valueOf(6 * 7);
  4. System.out.println(num); // prints 42
  5.  
If you'll pardon me, your statement of what you want seems a bit aimless. What I mean is: how do you intend using the % operator (for what purpose?), why do you want a String variable num?

I went and added so stuff but I don't know if it is correct.
I would suggest not writing code whose correctness is unknown. Throwing stuff at a wall in the hope that some of it might stick is futile and quickly becomes frustrating.

Instead:

(1) Figure out an algorithm (a plain English description of the steps to be followed) that will implement the isPrime() method you used in your original post. If you are unsure about the correctness of that algorithm then state it and you will have provided a precise and purposeful question.

(2) Consult your textbook/teacher/classmates etc and decide on the overall program structure - what methods the class will have, their argument and return types and their purpose. At this point the compiler will detect any mistakes of Java syntax and, if you can't understand them, you will again have things to post and precise questions to ask. Ie, post the code and the exact and entire compiler message. I would add that it is a good idea to compile early and often so that the syntax errors can be corrected one at a time as they occur.

(3) Once you have running code, test it. If the runtime behaviour is not what you intend then, once again, you have something to post (the code and a description of the behaviour) and a precise question to ask. (why did this occur?).

Good luck!
Mar 27 '10 #4
jkmyoung
2,057 Expert 2GB
if BigNumber % divisor == 0, then when you divide the BigNumber by divisor you get no remainder. That is, the divisor divides BigNumber evenly.

-> If the divisor is smaller than the number, then BigNumber is not prime.

?Do you know how to use functions? (eg like you have with isPrime(number))
Mar 29 '10 #5
anmoldhima5
2 2Bits
A number is prime number if it is only divided by 1 or itself . So we iterate loop 2 to Math.sqrt(number ) and check it
Below i have shared code for this
Expand|Select|Wrap|Line Numbers
  1. public class HelloWorld{
  2.  
  3.      public static void main(String[] args) {
  4.       int n =7;
  5.       if(isPrimeNumber(n)){
  6.             System.out.println("number "+ n +" is prime");
  7.            }else{
  8.            System.out.println("number "+ n +" is not prime");
  9.         }
  10.      }
  11.  
  12.      public static boolean isPrimeNumber(int n) {
  13.          int j = (int) Math.sqrt(n);
  14.          for(int i=2 ; i <= j ; i++){
  15.          if(n%i==0)
  16.          {
  17.              return false;
  18.          }
  19.          }
  20.         return true;
  21.      }
  22.  
  23. }
Source : Check number is prime or not
Jan 20 '21 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

31 posts views Thread by Chris S. | last post: by
4 posts views Thread by daniel.w.gelder | last post: by
13 posts views Thread by Stumped and Confused | last post: by
4 posts views Thread by Gianluca | last post: by
44 posts views Thread by gregory.petrosyan | last post: by
5 posts views Thread by Martin P. Hellwig | last post: by
1 post views Thread by ladislav | last post: by
1 post views Thread by EquinoX | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.