Computer Applications

Write a program to accept any 20 numbers and display only those numbers which are prime.

Hint: A number is said to be prime if it is only divisible by 1 and the number itself.

Java

Java Nested for Loops

71 Likes

Answer

import java.util.Scanner;

public class KboatPrimeCheck
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 20 numbers");
        for (int i = 1; i <= 20; i++) {
            int n = in.nextInt();
            boolean isPrime = true;
            for (int j = 2; j <= n / 2; j++) {
                if (n % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) 
                System.out.println(n + " is a Prime Number");
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

29 Likes


Related Questions