55.6k views
1 vote
The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); }.A) The code snippet will not compile.

B) The code snippet will display the desired result.
C) The code snippet will display an incorrect result.
D) The code snippet will loop forever.

1 Answer

0 votes

Answer:

B)

Step-by-step explanation:

The Code Snippet is correct and gives the expected result.

Prime numbers are numbers with only two divisors (1 and themselves)

Below are the list of prime numbers from 1-100

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

Testing each of the following numbers in the compiled code snippet gives the expected output.

User Rojalin Sahoo
by
5.8k points