Write a program to accept any 20 numbers and display only those numbers which are prime.
Share
Write a program to accept any 20 numbers and display only those numbers which are prime.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Question:-
Write a program to accept 20 numbers and display only those numbers which are prime.
Program:-
import java.util.*;
class Prime
{
static boolean isPrime(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
return (c==2);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 20 elements....");
int a[]=new int[20];
for(int i=0;i<20;i++)
{
System.out.print(">> ");
a[i]=sc.nextInt();
}
System.out.print("Prime numbers are: ");
for(int i=0;i<20;i++)
{
if(isPrime(a[i]))
System.out.print(a[i]+" ");
}
}
}