Computer Science, asked by Anonymous, 3 months ago

Input 20 no. In single dimensional Array & check it is prime no. or not

JAVA ​

Answers

Answered by Oreki
2

import java.util.Scanner;

public class PrimeArray {

   static boolean isPrime(int number) {

       for (int i = 2; i < Math.round(Math.sqrt(number)) + 1; i++)

           if (number % i == 0)

              return false;

       return true;

   }

   public static void main(String[ ] args) {

       System.out.println("Enter 20 numbers - ");

       // Accepting 20 numbers into a SDA.

       int[ ] numbers = new int[20];

       for (int i = 0; i < numbers.length; i++)

           numbers[i] = new Scanner(System.in).nextInt( );

       // Checking if prime or not

       System.out.println("\nNumbers checked if prime or not - ");

       for (int number : numbers)

           System.out.printf("%d - %s\n", number, isPrime(number) ? "Is Prime" : "Not Prime");

   }

}

Similar questions