简体   繁体   中英

find all prime numbers from array

I want to create a program that will ask the user to input 5 integers using array and determine all the prime numbers entered. But I have difficulty with it. What seems to be the problem? I use JCreator for this.

import java.util.Scanner;
public class PrimeNumbers{
public static void main (String[] args){
    int[] array = new int [5];
    Scanner in = new Scanner (System.in);

    System.out.println("Enter the elements of the array: ");
    for(int i=0; i<5; i++)
    {
        array[i] = in.nextInt();
    }
    //loop through the numbers one by one
    for(int i=0; i<array.length; i++){
        boolean isPrime = true;

        //check to see if the numbers are prime
        for (int j=2; j<i; j++){

            if(i%j==0){
                isPrime = false;
                break;
            }
        }
        //print the number
        if(isPrime)

            System.out.println(i + " are the prime numbers in the array ");
    }
}
}

You are checking the loop counters, not the values in the array. Try something like

for (int j=2; j<array[i]; j++){
    if(array[I]%j==0){
            isPrime = false;
            break;
        }

I haven't tested this.

UPDATE

To print out the results either print each on as it is found, or, copy the prime numbers into an output array and then print that when you have finished the checks. The details will depend on the language you are using.

Please note, you are not using a very efficient detection algorithm; Google for a better one.

You can check all integer numbers until root of the required number

Pseudo code:

 for(i=2; i<sqrt(number);i++){
  if(number/i===0){
    //not prime number
  }
}

Find all prime numbers from the array.

         package com.myfirstproject;

public class array_Uni_work {
public static void main(String[] args) {
    int[] array = {6,48,47,7 , 98,51,87,99,2,0,1,191,157};

    // loop through the numbers one by one
    for (int i = 0; i < array.length; i++) {
        boolean isPrime = true;
        if (array[i] == 1)
            isPrime = false;
        else {
            // check to see if the numbers are prime
            for (int j = 2; j <= array[i] / 2; j++) {
                if (array[i] % j == 0) {
                    isPrime = false;
                    break;
                }
            }
        }
        // print the number
        if (isPrime){
            if (array[i] == 0){}
            else {
                System.out.print(array[i] + " , ");
            }
    }}
    System.out.println(" Are the prime number in the array ");
}

}

public static void main(String[] args) {
        int[] array = new int[5];
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the elements of the array: ");
        for (int i = 0; i < 5; i++) {
            array[i] = in.nextInt();
        }
        // loop through the numbers one by one
        for (int i = 0; i < array.length; i++) {
            boolean isPrime = true;
            if (array[i] == 1)
                isPrime = false;
            else {
                // check to see if the numbers are prime
                for (int j = 2; j <= array[i] / 2; j++) {
                    if (array[i] % j == 0) {
                        isPrime = false;
                        break;
                    }
                }
            }
            // print the number
            if (isPrime)
                System.out.println(array[i] + " is a prime number in the array ");
        }
    }

Here is more efficient code finding prime number. We only need to check the odd number up to the square root of N, assume the number is greater than 2.

boolean isPrime(int n) {
        //check if n is a multiple of 2
        if (n%2==0) return false;
        //if not, then just check the odds
        for(int i=3;i*i<=n;i+=2) {
            if(n%i==0)
                return false;
        }
        return true;
    }

This is the simplest form of finding the prime numbers from the given array. We can also use scanner by assigning n instead of an array to check whether if it is a prime or non prime from the given input(commented in program).Hope this helps you..!!

public static void main(String[] arg) {
    int a[]= {1,2,3,4,5,6,7,8,9,10}; //int n=0 or n=values       
    int num =0;
    String  primeNumbers = "";
    for (int i = 0; i <= a.length; i++)  /*change a.length to n*/      
      {                   
         int counter=0;           
         for(num =i; num>=1; num--)
         {
        if(i%num==0)
        {
        counter = counter + 1;
        }
     }
     if (counter ==2)
     {
        //Appended the Prime number to the String
        primeNumbers = primeNumbers + i + " ";
     }  
      } 
      System.out.println("Prime numbers from given array :");
      System.out.println(primeNumbers);
   }
}

Since you have asked for 5 number explicitly, the code has hardcoded for 5 numbers

import java.util.Scanner;

public class PracticeTest {

  public static void main(String args[]) {
    int arr[] = new int[5];
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter 5 numbers");
    for (int i = 0; i < 5; i++) {
      arr[i] = sc.nextInt();
    }
    checkPrimeNumbers(arr);
  }

  public static void checkPrimeNumbers(int arr[]) {
    loop1:
    for (int j = 0; j < arr.length; j++) {
      loop2:
      for (int i = 2; i < arr[j]; i++) {
        if (arr[j] % i == 0) {
          continue loop1;
        }
      }
      System.out.print(arr[j] + " ");
    }
  }
}

This is the simplest way to accept 'n' integers and display the prime and non prime numbers present in that array.

import java.util.Scanner;
// Author Dot-Coin
public class The_Prime {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of the array");
        int size=sc.nextInt();
        int Primer[]=new int [size];
        int Non_Primer[]=new int [size];
        int Numbers[]=new int [size];
        int counter=0;int j=0,k=0,m=0,n=0;
        System.out.println("Enter the numbers ");
        for(int i=0;i<size;i++)
            Numbers[i]=sc.nextInt();
        for(int i=0;i<size;i++)
        {
            if(Numbers[i]==1||Numbers[i]==0)
            {
                continue;
            }
            
                for(n=1;n<=Numbers[i];n++)
                    {
                    if(Numbers[i]%n==0)
                        counter++;
                    }
                if(counter>2)
                {
                    Non_Primer[k]=Numbers[i];k++;counter=0;continue;
                }
                if(counter==2)
                {
                    Primer[m]=Numbers[i];m++;counter=0;
                }
            }
         System.out.println("The prime numbers are");
         for(k=0;k<Primer.length;k++)
                {
                    if(Primer[k]==0)
                        {
                        j++;continue;
                        }
                    else
                    System.out.println(Primer[k]);
                }
            if(j==Primer.length)
                System.out.println("Null");j=0;
            System.out.println("The Non prime numbers are ");
        for( k=0;k<Non_Primer.length;k++)
        {
            if(Non_Primer[k]==0)
                {
                j++;continue;
                }
            System.out.println(Non_Primer[k]);
        }
        if(j==Non_Primer.length)
            System.out.println("Null");
    }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM