Python code to find given number is prime or not? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Python code to find given number is prime or not?

Please provide a simple and explanatory code. It's a humble request.

2nd Sep 2023, 9:27 AM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
24 Answers
+ 6
S2XPHOENIX🇮🇳 Providing `readymade` code, this feature is not available here. its humble request First Try by yourself,after this if you face any problems in during learning or making code then ask here.
2nd Sep 2023, 9:41 AM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 6
Yosef Sahle thanks, but by simply providing an answer S2XPHOENIX🇮🇳 missed an oppertunity to collaborate with a mentor to learn new skills and understanding of the fundamentals needed to complete the code task on their own.
4th Sep 2023, 1:01 AM
Chris Coder
Chris Coder - avatar
+ 3
First, it's important to know what a prime number means. This requires a basic understanding of math. Secondly, which formula can you use to figure out if a number is prime? Lastly, how do you put that formula into your code to make it work? The last part requires an understanding of the basic fundamentals.
2nd Sep 2023, 4:59 PM
Chris Coder
Chris Coder - avatar
+ 3
S2XPHOENIX🇮🇳 Have you tried the search feature in the app? You will find that others have a similar question to yours. There are code samples available too. I would like to help you, but I feel that providing a code will be redundant, as you have various resources at your disposal. May I suggest you share your code attempt and comment on each part, explaining your thinking? This will help me understand your current knowledge and thought process. From there, I can suggest an alternative approach to complete the code based on your input.
2nd Sep 2023, 9:51 PM
Chris Coder
Chris Coder - avatar
+ 2
S2XPHOENIX🇮🇳 #update the app #Go on learn section & select course and read again with carefully... 1:Python Intermediate 2:Python introduction
2nd Sep 2023, 5:37 PM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 2
Here You Can Have the Code def check_prime(num): if num <= 1: return False for i in range(2, int(num**0.5) + 1): if num % i == 0: return False return True number = int(input("Enter a number: ")) if check_prime(number): print(number, "is a prime number.") else: print(number, "is not a prime number.")
3rd Sep 2023, 2:55 PM
Yosef Sahle
Yosef Sahle - avatar
+ 1
Here is a good explanation and walk thru of code: https://www.geeksforgeeks.org/prime-numbers/
2nd Sep 2023, 1:51 PM
Keith
Keith - avatar
+ 1
Chris Coder ik the number but I'm confused which all logics to implement here
2nd Sep 2023, 5:33 PM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
+ 1
Darpan kesharwani🇮🇳 link isn't working 🥲
2nd Sep 2023, 5:33 PM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
+ 1
Darpan kesharwani🇮🇳 but I've already completed the course
2nd Sep 2023, 5:47 PM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
+ 1
3rd Sep 2023, 5:19 AM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
+ 1
It first checks if num is less than or equal to 1, in which case it returns False since prime numbers are greater than 1 Next, it iterates through a range from 2 to the square root of num (inclusive) using a for loop. Within the loop, it checks if num is divisible by the current iteration value i using the modulo operator (%). If num is divisible by any value of i, it means that num is not prime and the function returns False. If the loop completes without finding any factors of num, it means that num is prime and the function returns True.
3rd Sep 2023, 3:06 PM
Yosef Sahle
Yosef Sahle - avatar
+ 1
If You want the simple one You Can Also Use this code but it will take 2x time since it iterates (num) times def check_prime(num): if num <= 1: return False for i in range(2, num): if num % i == 0: return False return True number = int(input("Enter a number: ")) if check_prime(number): print(number, "is prime.") else: print(number, "is not prime.")
3rd Sep 2023, 3:10 PM
Yosef Sahle
Yosef Sahle - avatar
+ 1
Thnx vro Yosef Sahle
3rd Sep 2023, 4:56 PM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
+ 1
Chris Coder sorry, i though i was helping and you are right that was a mistake
4th Sep 2023, 5:46 AM
Yosef Sahle
Yosef Sahle - avatar
+ 1
#taking value from user try: num = int(input("Enter the number: ")) except ValueError: print("Please enter the Integer value!") exit() isPrime = True if num<2: isPrime = False else: for i in range(2, num): if num%i == 0: isPrime = False break if isPrime: print(num, "Number is Prime number.") else: print(num, "Number is not a Prime number.")
11th Nov 2023, 4:24 AM
Vyankatesh Odilwar
Vyankatesh Odilwar - avatar
0
Darpan kesharwani🇮🇳 But I'm confused which logic to implement here 🥲
2nd Sep 2023, 11:15 AM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
0
Yosef Sahle could u pls explain the code.
3rd Sep 2023, 2:59 PM
S2XPHOENIX🇮🇳
S2XPHOENIX🇮🇳 - avatar
0
am having troubles finding the next course to take after python intermediate, coz this "python developer course" that i thought was supposed to be next turns out it a combination of introduction & intermediate python courses, so i would appreciate any help/guidance from anyone🙏thnx!
4th Sep 2023, 6:22 AM
Mara Martinez
Mara Martinez - avatar