Python

# Python While Loop To Find Prime Does Not Terminate2 min read

Here is the python code:

Output:

The above code is a Python script that determines if a whole number entered by the user is a prime number or not. The script starts by asking the user to input a whole number using the `input()` function. This input is then converted to an integer using the `int()` function and stored in the variable `num`.

The script then checks if the entered number is greater than 1. This is because a prime number must be greater than 1. If the entered number is greater than 1, the script enters a `for` loop that will iterate over a range of numbers starting from 2 and ending at the entered number.

For each iteration, the script checks if the entered number is divisible by the current iteration number. This is done using the modulo operator (`%`), which returns the remainder when the entered number is divided by the current iteration number.

You may also like: Python Code Examples

If the remainder is 0, this means that the entered number is divisible by the current iteration number and therefore not a prime number. In this case, the script prints a message saying that the entered number is not a prime number and then breaks out of the loop using the `break` statement.

On the other hand, if the remainder is not 0, this means that the entered number is not divisible by the current iteration number. The loop then continues to the next iteration and checks again. If the loop completes all its iterations without finding any divisors, this means that the entered number is a prime number.

In this case, the `else` clause associated with the `for` loop is executed and a message saying that the entered number is a prime number is printed.

Finally, if the entered number is not greater than 1, the script prints a message saying that the entered number is not a prime number.

In summary, this code takes a whole number input from the user, checks if it is greater than 1, and then uses a `for` loop and the modulo operator to determine if the number is a prime number or not. If the entered number is prime, the script prints a message saying so, and if not, it prints a message saying that the entered number is not a prime number.