Page Contents
Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
Prime number program in python with explanation
Ans:
In this tutorial we are going to learn how to write a program to check whether a given integer number by user is a prime number or not in Python programming language.
If 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 the writing prime number program in c, first you should know
What is prime number?
In Mathematical term, A prime number is a number which can be divided by only 1 and number itself.
For example : 2, 3, 5, 7, 13,…
Here 2, 3 or any of the above number can only be divided by 1 or number itself.
How our program will behave?
Suppose if someone gives an input 2 then our program should give output “given number is a prime number”.
And if someone gives 4 as an input then our program should give output “given number is not a prime number”.
Python program to check given number is prime or not
i,temp=0,0
n = int(input("please give a number : "))
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:
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program