Computer Science, asked by ashi200695, 11 hours ago

write a program to create a single dimensional array of N integers print only those integers from the array which are prime numbers​

Answers

Answered by samarthkrv
0

Answer:

import java.util.Scanner;

public class Main

{

   static boolean isPrime(int n){

       for(int i = 2; i < n; i++){

           if(n%i == 0){

               return false;

           }

       }

       return true;

   }

public static void main(String[] args) {

 Scanner sc = new Scanner(System.in);

 System.out.print("Enter the number of elements in the array:");

 int n = sc.nextInt();

 int[] arr = new int[n];

 System.out.println("Enter all " + n + " numbers");

     for(int i = 0; i < n; i++){

         arr[i] = sc.nextInt();

     }

 System.out.println("---ORIGINAL ARRAY---");

     for(int i = 0; i < n; i++){

         System.out.print(arr[i] + " ");

     }

 System.out.println("\n---PRIME NUMBERS IN THE ARRAY---");

     for(int i = 0; i < n; i++){

         if(isPrime(arr[i])){

             System.out.print(arr[i] + " ");

         }

     }

}

}

Explanation:

Answered by requize17
2

Answer:

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();

}

for(int i=0; i<array.length; i++){

boolean isPrime = true;

for (int j=2; j<i; j++){

if(i%j==0){

isPrime = false;

break;

}

}

if(isPrime)

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

}

}

}

Similar questions