WAP in java to store 20 numbers in a Single Dimentional Array( SDA).Display the numbers which are prime.(Please write this without using any array functions i.e.isPrime etc)
Answers
Answer:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter how many Numbers:");
int number = in.nextInt();
int [] array=new int[number];
for (int i=0;i<array.length;i++ )
{
System.out.print("Enter number:");
CheckPrimeorNot(in.nextInt());
}
}
public static void CheckPrimeorNot(int n){
int i,m=0,flag=0;
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is prime number"); }
}//en
}
}
Explanation: