In this tutorial, we are going to learn how to write a program to check whether a given integer number by the user is a prime number or not in Python programming language.
If a given number is prime then our logic will assign value 0 to a temp variable and will print “number is prime” and if the number is not prime then our logic will assign value 1 to a temp variable program will print “number is not prime”.
Before directly moving on to the writing prime number program in Python Lets know
What is Prime Number?
In Mathematical terms, A prime number is a natural number greater than 1 that can be divided by only 1 and the number itself.
For example 2, 3, 5, 7, 13… are Prime numbers.
Here 2, 3, or any of the above numbers can only be divided by 1 or the number itself. So we can say that numbers are prime numbers.
How our program will behave?
Suppose someone gives an input 2 then our program should give the output “given number is a prime number”.
And if someone gives 4 as an input then our program should give the output “given number is not a prime number”.
There are various methods to write the program. In this post, we will see multiple logics.
Method 1: Check Prime Using For Loop
n = int(input("please give a number : "))
i,temp=0,0
for i in range(2,n//2):
if n%i == 0:
temp=1
break
if temp == 1:
print("given number is not prime")
else:
print("given number is prime")
Output:
please give a number : 23
given number is prime
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…