Broke Samith
Broke Samith

Reputation: 21

Java isPrime function: Check if input integer values are prime

In short, again I found the task on internet:

Input integers from the keyboard, check whether prime numbers or not. When you input 0, the program ends.

So far, I wrote the logic for checking the integer if it's prime or not. The main stumbling block was that I should read several integers from one string and stop program if last integer is 0. So when I tried to add a loop to iterate over the input and check whether the integer is prime or not, my logic doesn't work and it returns only 1 st integer without others.

import java.util.Scanner;

public
class PrimeNumber
{

public
    static void main(String[] args)
    {
        int temp;
        boolean isPrime = true;
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int[] arr = new int[num];
        for (int i = 0; i < arr.length; i++)
        {
            arr[i] = sc.nextInt();

            for (int y = 2; y <= num / 2; i++)
            {
                temp = num % i;
                if (temp == 0)
                {
                    isPrime = false;
                    break;
                }
            }
        }

        if (isPrime)
            System.out.println(num + " is a Prime Number");
        else
            System.out.println(num + " is not a Prime Number");
    }
}

Upvotes: 0

Views: 962

Answers (3)

Huntbook
Huntbook

Reputation: 148

Your thought process was good.

The snippet with the if-else statement is outside of the for loop, so it will only happen once.

The value of num is just the number of int values the user will type in (basically arr.length). It should instead be checking primality of arr[i]. If arr[i] is divisible by some number other than 1 or arr[i], it is not prime. Also, if arr[i] is not greater than 1, it is not prime, either.

Lastly, make sure isPrime gets reset to true.

I recommend adding instructions in the form of print(), so it becomes clearer which number is which:

public static void main(String[] args)
    {
        int temp;
        boolean isPrime;
        Scanner sc = new Scanner(System.in);
        System.out.print("Number of integer values: ");
        int numberOfInts = sc.nextInt();
        int[] arr = new int[numberOfInts];
        for (int i = 0; i < numberOfInts; i++)
        {
            isPrime = true;
            System.out.print("Int " + (i+1) + " = ");
            arr[i] = sc.nextInt();
            for (int y = 2; y <= arr[i] - 1; y++)
            {
                temp = arr[i] % y;
                if (temp == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            if (arr[i] <= 1) {
                isPrime = false;
            }

            if (isPrime)
                System.out.println(arr[i] + " is a Prime Number");
            else
                System.out.println(arr[i] + " is not a Prime Number");
        }
    }

As for exiting the program, put this at the start of the enclosing for loop:

if (arr[i] == 0) {
    break;
}

There are better solutions than this, but on a basic level, this is fine.

Upvotes: 1

WOSAJ
WOSAJ

Reputation: 21

You can try using this function to check that the number is prime:

  private static boolean isPrime(int number) {
      return java.math.BigInteger.valueOf(number).isProbablePrime((int) Math.log(number));
  }

Then you can iterate through the array and check each of its elements with this function

My version of this program:

  public class Main {
    public static void main(String[] args) {
      try(java.util.Scanner sc = new java.util.Scanner(System.in)) { //use try-with-resources block to avoid resources leak
        //filling an array
        int[] ints = new int[sc.nextInt()];
        //if program gets 0, it stops filling
        for(int i = 0; i < ints.length; i++) {
            int temp = sc.nextInt();
            if(temp == 0) break;
            ints[i] = temp;
        }
        //check
        for(int i = 0; i < ints.length; i++) 
          if(ints[i] != 0) System.out.printf("%s is%s a prime number.\n", ints[i], isPrime(ints[i]) ? "" : "n't");
    }
  }
     
  private static boolean isPrime(int number) { //Some maths
    return java.math.BigInteger.valueOf(number).isProbablePrime((int) Math.log(number));
    }
  }

Upvotes: 0

benjibogush
benjibogush

Reputation: 13

I recommend if you can check this link: Check if an int is prime Java

Write a boolean function that checks an integer if it is prime or not. You do not need to create a O N^2 algorithm from input box.

Step 1: Check one integer and see if returns true( means it is prime )

Step 2: Try random numbers and see if they work as well.

Step 3: Add an array and see if contents of the array has prime numbers or not. Then you can print out any message you like.

See this example from the same reference to get started. https://onecompiler.com/java/3y2cxy9ea

Upvotes: 1

Related Questions