write a program that input 20 numbers in an array and print only the prime numbers. in a single dimensional solved
Answers
Answered by
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:
- Accepting the numbers.
- Looping through numbers in the array.
- Count the number of factors of each number.
- If number of factors == 2, display the number.
Refer to the attachment for output.
•••♪
Attachments:
Similar questions
Accountancy,
27 days ago
India Languages,
27 days ago
Math,
1 month ago
India Languages,
8 months ago
India Languages,
8 months ago