vendredi 26 juin 2020

To print prime numbers from the array in JAVA

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.

package arrays;

import java.util.Scanner;

public class Examples {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in) ;
        System.out.println("Total numbers : ");
        int n = sc.nextInt();
        boolean isPrime =true;
        int result =0;
        System.out.println("Enter the Numbers : ");
        int [] numbers = new int[n];
        
        for(int i=0;i<n;i++) {
            numbers[i]=sc.nextInt();
        }
        for(int i=0;i<n;i++) {
            for(int j=2;j*j<numbers[i];j++) {
                if(numbers[i]%j ==0) {
                    isPrime =false ;
                    break;  
                }
            }
        }
        if(isPrime) {
            System.out.println("The Prime Numbers : " +numbers[i]);
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire