How to find prime number from a input array .
In this java program we will take a array size then input array . The program
will tell us which number is prime or which is not . Program is simply taking
two loop to compare .
import java.util.Scanner;
public class Primeinarray {
public static void main (String[] args){
System.out.println("Enter the elements of the array length: ");
Scanner in = new Scanner (System.in);
int[] array =new int[in.nextInt()];
System.out.println("Enter the elements of the array: ");
for(int i=0;array.length; i++)
{
array[i] = in.nextInt();
}
//loop through the numbers one by one
boolean isPrime = true;
for( int i=0 ; i<array.length; i++) {
if (array[i]==2) {
if (array[i]==2) {
System.out.println("2 is prime number")
}
for(int j=2; j<array[i]; j++){
if(array[i]%j==0){
isPrime = false;
System.out.println( array[i] +"Not an prime number");
break;
} else {
System.out.println( array[i] +" prime number");
break ;
}
}
}
}
}
Enter the elements of the array length:
Enter the elements of the array length:
3
Enter the elements of the array:
3
2
4
3 prime number
2 is prime number
4Not an prime number
How to find prime number from a input array .
Reviewed by Mukesh Jha
on
12:53 AM
Rating:
No comments:
Add your comment