Hello people, welcome back to Programming In Python! Here I will discuss a Python program that finds whether a given number is a prime number or composite number or neither of them.
Definition: A number that is greater than 1 is said to be prime if it has no other factors other than 1 and itself. The numbers 0 and 1 are neither prime nor composite. And remaining all numbers are composite numbers.
Master the basics of data analysis in Python. Expand your skillset by learning scientific computing with numpy.Ad:Limited time sale - 50% OFF Learn in-demand data and AI skills on the 17th of July
You can also watch the video on YouTube here.
Prime or Composite – Code Visualization
To find whether a number is a prime or composite number.
- Read input number using
- Check if num is greater than 1.
- Find factors
- Run a for loop ranging from 2 to the num entered.
- check if num divided by any number gives a remainder 0.
- if it gives a remainder of 0, the number is not a prime number.
- if not, the number is a prime number.
- If the number entered is either 0 or 1, we say that the number is neither prime nor composite number.
- All other numbers are composite numbers.
- Print the result.
num = int(input("Enter any number : ")) if num > 1: for i in range(2, num): if (num % i) == 0: print(num, "is NOT a prime number") break else: print(num, "is a PRIME number") elif num == 0 or 1: print(num, "is a neither prime NOR composite number") else: print(num, "is NOT a prime number it is a COMPOSITE number")
You can find more math-related Python programs below.
- Layout Managers in Python GUI – Pack, Grid and Place
- A simple python GUI calculator using TKInter
- Introduction to Python GUI – Labels, Frames, Buttons
- How to create a Domain to IP converter in Python GUI using TKInter
- How to create a temperature converter app in python GUI using TKInter