Sunday, 26 April 2020

Write a program in Java to store 20 numbers in a Single Dimensional Array (SDA). Display the numbers which are prime.

import java.util.Scanner;
public class ATS
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter 20 numbers");
for (int i = 0; i < arr.length; i++)
{
arr[i] = in.nextInt();
}
System.out.println("Prime Numbers:");
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == 1)
continue;
boolean isPrime = true;
for (int j = 2; j <= arr[i] / 2; j++)
{
if (arr[i] % j == 0)
{
isPrime = false;
break;
}

}
if (isPrime)
System.out.print(arr[i] + ", ");

}
}
}

No comments:

Post a Comment