Computer Science, asked by aarzoogulzar31, 1 month ago

write a program that input 20 numbers in an array and print only the prime numbers. in a single dimensional solved ​

Answers

Answered by anindyaadhikari13
1

Answer:

The given program is written in Java.

import java.util.*;

public class Array  {

   public static void main(String args[])  {

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter 20 elements..");

       int a[]=new int[20],i,c;

       for(i=0;i<20;i++)   {

           System.out.print(">> ");

           a[i]=sc.nextInt();

       }

       sc.close();

       System.out.println("Prime numbers are..");

       for(int x:a)    {

           c=0;

           for(i=1;i<=x;i++){

               if(x%i==0)

                   c++;

           }

           if(c==2)

               System.out.print(x+" ");

       }

   }

}

Algorithm:

  1. Accepting the numbers.
  2. Looping through numbers in the array.
  3. Count the number of factors of each number.
  4. If number of factors == 2, display the number.

Refer to the attachment for output.

•••♪

Attachments:
Similar questions