I have used three variables in this program namely *number*, *counter* and *decider*. All of them are of *integer* type. *number* is used to store the number entered by the user which is to be checked. *counter* is initialized at 2 and then it is incremented by 1 in each loop and it is used to test the division of *number* against all integers between 2 and the number itself but excluding itself also. The so called variable *decider* is used to make decision during the last steps of the program while it is being told to the user that the number he enetered is a prime number.

The number entered by the user is also tested once to make sure that it is a positive number number. If it is not hten the user is promted to enter the number a second time. Hoewver, if the user again enters a negative number then now he will get a wrong output. So it is important to enter only positive numbers while using this program.

If number entered by user is either 0 or 1 then he simply gets a message that the number entered by him is neither prime nor composite.

However, if the number entered is greater than or equal to 2 then testing will start from the *while loop*. In each iteration of this loop, remainder will be calculated when number is divided by counter. If this remainder equals zero then user will be told that the number entered is not a prime number, the variable *decider* will be incrimented by 1 and the loop will exit after the *break* statement is encountered. But if the remainder *number % counter* is not equal to zero then the variable *counter* will be incremented by 1 and the loop will start again. As soon as the the variable *counter* will become equal to the variable *number*, the execution of the loop will stop. After the loop, their is an *if* statement. It tests the variable *decider* if it is equal to zero or not. If it is, then, user will be told that the entered number is a prime number. Please not that *decider* will not eual zero (i.e. it will be greater than zero) only if it has been tested and told to the user that number is not prime. In this case, it ofcourse makes sense to skip the statements inside this last *if* statement. If you still have any confusion about any step of this program or you have some alternative way of doing any of these steps then please feel free to share it with me via comments. I shall be very thankful to you. Continue reading →