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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlock784
    New Member
    • Feb 2010
    • 5

    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.JOp tionPane;

    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.sho wInputDialog("E nter a number.");
    number = Integer.parseIn t(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.sho wMessageDialog( null, message);

    System.exit(0);
    }

    }
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Alright, where is the code for your isPrime function? What algorithm do you intend to use?

    Comment

    • jlock784
      New Member
      • Feb 2010
      • 5

      #3
      isPrime

      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.JOp tionPane;

      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.sho wInputDialog("E nter a number.");
      number = Integer.parseIn t(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.sho wMessageDialog( null, message);

      System.exit(0);
      }

      }

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        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.

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

        Code:
        String num = "Hello world";
        System.out.println(num); // prints Hello world
        num = String.valueOf(6 * 7);
        System.out.println(num); // prints 42
        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!

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          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) )

          Comment

          • anmoldhima5
            New Member
            • Jan 2021
            • 2

            #6
            A number is prime number if it is only divided by 1 or itself . So we iterate loop 2 to Math.sqrt(numbe r ) and check it
            Below i have shared code for this
            Code:
            public class HelloWorld{
            
                 public static void main(String[] args) {
                  int n =7;
                  if(isPrimeNumber(n)){
                        System.out.println("number "+ n +" is prime");
                       }else{
                       System.out.println("number "+ n +" is not prime");
                    }
                 }
            
                 public static boolean isPrimeNumber(int n) {
                	 int j = (int) Math.sqrt(n);
                	 for(int i=2 ; i <= j ; i++){
                	 if(n%i==0)
                	 {
                		 return false;
                	 }
                	 }
            		return true;
                 }
                 
            }
            Source : Check number is prime or not

            Comment

            Working...