🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, if a number is prime, it cannot be divided evenly by any other number than 1 and itself. Understanding and checking for prime numbers is a foundational topic in number theory and has applications in areas such as cryptography.
In this blog post, we will learn how to write a Python program to check if a number is a prime number.
2. Program Overview
The Python program that we'll create will follow these steps:
1. Take an input number from the user.
2. Check if the number is less than 2. If yes, it's not prime.
3. For numbers 2 and above, use a loop to check for factors other than 1 and the number itself.
4. Display whether the number is prime or not.
3. Code Program
# Taking input from the user
num = int(input("Enter a number: "))
# Initialize a flag variable
is_prime = True
# Prime numbers are greater than 1
if num > 1:
# Check for factors
for i in range(2, int(num**0.5)+1):
if (num % i) == 0:
is_prime = False
break
else:
is_prime = False
# Output the result
if is_prime:
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
Output:
Enter a number: 29 29 is a prime number
4. Step By Step Explanation
1. We begin by taking input from the user. The input() function returns a string, so we convert this to an int.
2. We initialize a boolean variable is_prime to True. This variable will help us determine the primality of our input number.
3. If the number is greater than 1, we proceed to check for factors. To optimize the process, we only iterate up to the square root of the number (num0.5) because a larger factor of the number must be a multiple of a smaller factor that has been already checked.
4. If we find any number between 2 and num that divides num evenly (num % i == 0), then num is not a prime number.
5. If the number is less than 2 or if a factor is found, is_prime is set to False.
6. Finally, we use the value of is_prime to display the appropriate message to the user.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment