Home > other >  How do I get an if statement to check if a user given number is a prime number?
How do I get an if statement to check if a user given number is a prime number?

Time:05-04

I am writing a very basic program in java to check if a number given by the user is a prime number.

I have tried to use an if statement to check if the number divided by itself is equal to 1 as well as if it is divided by 1 it equals itself, but when I run the program and enter a number there is no reaction at all from the if statement.

package prime.java;

import java.util.Scanner;

public class Prime {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Welcome to Prime!\nPlease enter a number:");
        
        Scanner Scan = new Scanner (System.in);
        int number = Scan.nextInt();
        System.out.println(number);
        
        
        
        if (number%1 == number && number%number == 1) {
            System.out.println(number   " is a prime num");
        }
    }

}

Am I using the right operators? I'm a java beginner so I appreciate any tips or advice :)

Thanks!

CodePudding user response:

This can help you:

static bool isPrime(int number)
    {
        for (int i = 2; i < number; i  )
        {
            if((number% i) == 0)
            {
                // Not Prime
                return false;
            }
        }

        // Just Prime!
        return true;
    }

A prime number is not divisible by any numbers between 2 and (itself - 1).

This way, if you call 'isPrime()' inside the if, this just works.

Hope this can help you!

CodePudding user response:

, but when I run the program and enter a number there is no reaction at all from the if statement.

This is because the if condition is never fulfilled ( for example : n%n will be 0 always) .

Thus, you can keep a variable i that starts from 2 till number -1 and check if that number divided by i , the remainder should never be 0 .

public class PrimeNumber {
    public static void main(String[] args) {
        int k = 5;
        boolean isPrime = true;
        for (int i = 2; i < k; i  ) {
            if (k % i == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) {
            System.out.println("number "   k   " is prime");
        } else {
            System.out.println("number "   k   " is not prime");

        }
    }

}

and the output is :

number 5 is prime

CodePudding user response:

You can write a isPrime function to check if a given number is a prime or not, like below

public static boolean isPrime(int n) {
    // Corner case
    if (n <= 1) {
        return false;
    }

    // Check from 2 to n-1
    for (int i = 2; i < n; i  ) {
        if (n % i == 0) {
            return false;
        }
    }

    return true;
}

And replace your

if (number%1 == number && number%number == 1)

to

if (isPrime(number)

CodePudding user response:

This is more efficient:

  boolean isPrime(int number) {
    int root = (int) Math.sqrt(number);
    for (int i = 2; i <= root; i  ) {
      if ((number % i) == 0) {
        return false;
      }
    }

    return true;
  }

CodePudding user response:

Looks like someone is doing a project Euler problem.

You are close but I would suggest maybe looking into how you check if something actually is a prime number as the other comment suggested.

Research also some specifics of prime numbers, I will give one away which is : no prime number ends with 5. Implementing such rules can drastically reduce your programs run time.

And also you want to check out the correct usage of the modulo (%) operator.

 public static boolean isPrime(Long num){
    String number = String.valueOf(num);
    if(number.length() >= 2 ){
        //Lets see if a number ends with 5 if it does it is divisible by 2 and not prime
        if(number.endsWith("5")){
            return false;
        }
    }
    //No reason to check if a number is prime when it is 2.
    else if (num == 2){
        return true;
    }
    //Any number that can be devided by 2 with no remainder clearly isn't prime
    else if(num % 2 == 0){
        return false;
    }
    //All other numbers actually need to be checked.
    else{
        for (Long i = num -1; i > 2; i--) {
            if(num % i == 0){
                return false;
            }
        }
        }
        return true;
    }
  •  Tags:  
  • java
  • Related