WAP (Python) to find whether the number entered by the user is a prime number or not - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Sunday 20 September 2020

WAP (Python) to find whether the number entered by the user is a prime number or not



Hey all, today we are going to find whether the number entered by the user is a prime number or not. So be ready with your code editors as we are going to do this in the best way possible!

As always, if you want the full program, you can find it at the end of this article. But it would be good if you be with us this entire journey as this will help you in solving future problems that come your way. So let's start!!!

Overview:

If a number (>1) is not divisible by any other number except 1 and itself, then it is known as a prime number. The numbers belonging to this category are very random because they don’t occur in any specific pattern. These numbers are highly used in cryptography because of their randomness. 


For example: 5 cannot be divided by any other number except 1 and itself. Same is the case for 2, 3, 7, 11… and so on. Now you may think, is 1 a prime number? No, my friend, surprisingly, 1 is neither a prime nor a composite (non- prime) number.


But if its occurrence is random, then how do we write a code to find it? The answer is that its occurrence is random for us, but for computer, we can write a code that divides that number with all number preceding it to find whether it’s a prime number or not.


So, we would be using loops to divide the number from every number starting from 2 to the number preceding the concerned number.


Now that I have told you the methodology, maybe you could just try it out on your own. See if you use the same method!


Understanding what we need to do:

Here, I will tell you step wise what you need to do:


Step 1: Taking the input from the user:

In this step, we would take the number as an input from the user and save the value in a variable called ‘num’. Note that we have converted the input to ‘int’ because the default type of an input in python is string but we need to do calculations on it.


The code of this step is as follows:


Step 2: Finding if the number is a prime or not using loops:

Now, this process may look a bit difficult to you but trust me its very easy. To find whether the number is a prime number or not, we have to use looping statements to check if it is divisible by any number (else than 1 and itself).


We have used the ‘for’ loop for this program. We have defined a variable ‘i’ which would be used as a condition and the range is set from 2 to num. Note that the 'for' loops runs to a range of (start point) to (end point -1), hence we wrote end point to be num instead of (num -1).


Next, we are using the if statement to check if the number is divisible by i or not. If it is, then the statement written beneath it will be printed and the loop will break with the ‘break’ statement. Note that we used the ‘break’ statement because if we wouldn’t have written it, the loop would have kept on going and would print the statements multiple times for all the numbers.


If num is not divisible by any number in the range specified, then the loop will terminate and the ‘else’ would print the statement written under it. Note that, we didn’t put the else statement under the loop because if we did, it would have printed the statement multiple times for the numbers that aren't satisfied by the if statement. 


The code for this step is as follows:

Hence, we are able to find whether the number entered by the user is a prime number or not!


The final code looks something like this:

>


So, this was the final code. I hope you were able to not only learn the best way of solving this program, but also the best procedure that you should follow in solving any coding problem. 


Please subscribe to our newsletter for further updates of programming tutorials. If you have any doubts, feel free to ask in the comment section below. We will be happy to answer them! 


Your next coding challenge is:

WAP to print the nth term of the Fibonacci series.


So, we will meet again the next week and will discuss the answer to the above question. Till then keep coding!


Again, thanks for reading!!!


No comments:

Post a Comment