Python Interview Question 9/100! 🐍💻
#pythonprogramming #codingchallenge #pythondeveloper
Python Interview Question 9/100! 🐍💻
Challenge: Write a Python program to check if a user input is a Prime Number. 🔢
💡 Step-by-Step Code Explanation:
• Step 1: We handle the edge case right away. If the number is less than 2, it is automatically not a prime number.
• Step 2: We initialize a flag variable to 0. This will track whether we find any divisors.
• Step 3: A while loop is used to check if the number is evenly divisible by any integer starting from 2 (using the modulo operator %).
• Step 4: If a divisor is found (meaning the remainder is 0), we change the flag to 1 and use a break statement to exit the loop immediately, optimizing the code's performance.
• Step 5: Finally, if the loop finishes and the flag remains 0, the number is confirmed as prime.
Can you write this same logic using a for loop instead? Let me know your exact code in the comments! 👇
📌 Save this reel for your technical interview prep!
🚀 Follow for 100 Days of Python Questions!
#pythonprogramming #codingchallenge #pythondeveloper #techinterview #100daysofcode
Comments
Comments from the original post on Instagram
No comments yet
