Hey all, in this amazing tutorial, we are going to check if a number entered by the user is a palindromic-prime number or not. So gear up as we are going to do this program in the best way possible!
As always, you can find the final code for this problem at the end of this article. But I would suggest you to be with us this whole journey as we are going to go through each and every step you need to follow to become the best coder in the world. Excited? Let's start!
So, what is a palindromic-prime number?
A palindrome number is a number whose reverse is equal to the number itself.
For example: The number 151:
When we look at the number 151, it is same as itself from left and right. Hence, it is a palindrome number!
A prime number is a number which is not divisible by any other number than 1 and itself.
For example: In the case of 11:
The number 11 has no other factor other than 1 and itself. Hence, 11 is a prime number!
A palindromic-prime number is a number which is both a palindrome and a prime number simultaneously!
For example: The number 151:
The number 151 is a prime number as well as a palindrome number. Hence it satisfies both condition and becomes a palindromic-prime number!
I hope that you are now able to understand the basic definition of a palindromic-prime number. Now, let's start with the overview!
Overview:
In this question, we are asked to check if a number entered by the user is a palindromic-prime number or not. The only input required from the user would be the number.
We have already made a tutorial on how to tell if a number is a palindrome number and also for finding if a number is a prime number or not. So, we are not going to explain the procedure again, else this would become a lengthy tutorial.
Hence you are requested to see those tutorials first. We are not telling you to copy-paste those tutorials for this program, we are just asking you to read them once, so that you are able to understand the basics on what this program is being written.
Find those tutorials here:
1. WAP (Python) to find whether the number entered by the user is a prime number or not
2. WAP (Python) to check if a string/number is a palindrome or not
In this tutorial, we are going to intermix the understanding of these two programs to find the best possible solution to this problem! Although, we not just going to copy-paste code, we would have to do certain adjustments in them to solve this problem.
*As I have already told you the basics of this program, you could try to do this on your own. Check if the solution matches!*
Understanding what we need to do:
Here I will tell you step-by-step what you need to do.
Step 1: Taking the input from the user
This is the most easy part. We just have to take the input from the user. Also, don't forget to typecast the input into integer using int command as we need to perform calculations on it. We are going to store its value in a variable named 'num'.
Here's the code for this step:
Step 2: Checking if a number is a palindromic-prime number or not
First, we are going to check if the number is a palindrome or not. We can do this by using slice function, which disintegrates a string into its individual elements. We are going to disintegrate the number from back to front using the num[::-1] which will get each element from last index to first one.
Notice the word string we mentioned in the previous paragraph. You may remember that in the first step, we type casted the variable 'num' to int. Hence, to execute this statement, we need to add str along with the above code. We are going to store this value in variable named 'reverse'. Also, as we will again typecast 'reverse' to int as we need it for further reference.
Code will now look like:
Now, we would write an if statement to check if 'num' is equal to 'reverse' or not. If it is, then we would write code to check if a number is a prime number or not.
I am not going to explain the procedure to check if a number is a prime or not. You can check that in the link given in Overview.
In short, we are going to write a for loop which will divide the number from every number starting from 2 to number-1. If the number is divisible by any number, then its not a prime number, so the loop breaks. But if the number isn't divisible by any number in the range, then we can say that the number is a prime number.
*Note: One important step would be to check if the number is greater than 1 or not. As, 1 is neither a composite nor a prime number, checking this condition becomes necessary.*
If the number isn't a prime number, then we would tell the user that the number is not a prime but is a palindrome number. But if the number is a prime number, then we would tell user that the number is a prime as well as a palindrome number!
Now, the code would look somewhat like this:
Next, we would do the same check for prime number in the else statement, where the number isn't a palindrome number. The code would be the same. Only the print statements would change somewhat.
*Note: One important step would be to check if the number is greater than 1 or not. As, 1 is neither a composite nor a prime number, checking this condition becomes necessary.*
If the number doesn't come out to be a prime number, we would tell the user that the number is neither a prime nor a palindromic number. But if the number comes out to be a prime number, then we would tell the user that the number is a prime number but not a palindromic number.
*This was the final step of the code.*
Here's the final code:
I hope that you understood the code. If you didn't then please read the code line by line and try to play with it. It will help you to understand the significance of each line of code. This is the best way to learn coding.
But then also, the comments section is always open for your queries. We will try our best to address those queries as soon as possible.
Here's the output of the code:
This is the end of our amazing journey where we found the best way to check if a number is a palindromic-prime number or not!
I hope you learnt something new today. And if you did, then don't forget to share it with others as it may help someone struggling with coding!
Also, don't forget to subscribe to our newsletter as we do these coding session regularly!
Again, thanks for reading!
No comments:
Post a Comment