Python Programming

Prime Number

Showing prime number under 1 to 50 using function and for loop in Python

7/20/2021
0 views
prime-number.pyPython
#!/usr/bin/evn python

# Define function as 'prime' and pass parameter as 'no'
def prime(no):
	if no == 1:
		print ("1 is a Special!")
		# Return boolen value
		return False

	# For loop
	for x in range(2,no):
		if no % x == 0:
			print ("{} = {} x {}".format(no, x, no //x))
			# Return boolen value
			return False
	else:
		print no, "is a prime number!"
		# Return boolen value
		return True

# For loop
for a in range(1,50):

	# Call function 'prime' by passing parameter
	prime(a)

# ***** Output *****

# 1 is a Special!
# 2 is a prime number!
# 3 is a prime number!
# 4 = 2 x 2
# 5 is a prime number!
# 6 = 2 x 3
# 7 is a prime number!
# 8 = 2 x 4
# 9 = 3 x 3
# 10 = 2 x 5
# 11 is a prime number!
# 12 = 2 x 6
# 13 is a prime number!
# 14 = 2 x 7
# 15 = 3 x 5
# 16 = 2 x 8
# 17 is a prime number!
# 18 = 2 x 9
# 19 is a prime number!
# 20 = 2 x 10
# 21 = 3 x 7
# 22 = 2 x 11
# 23 is a prime number!
# 24 = 2 x 12
# 25 = 5 x 5
# 26 = 2 x 13
# 27 = 3 x 9
# 28 = 2 x 14
# 29 is a prime number!
# 30 = 2 x 15
# 31 is a prime number!
# 32 = 2 x 16
# 33 = 3 x 11
# 34 = 2 x 17
# 35 = 5 x 7
# 36 = 2 x 18
# 37 is a prime number!
# 38 = 2 x 19
# 39 = 3 x 13
# 40 = 2 x 20
# 41 is a prime number!
# 42 = 2 x 21
# 43 is a prime number!
# 44 = 2 x 22
# 45 = 3 x 15
# 46 = 2 x 23
# 47 is a prime number!
# 48 = 2 x 24
# 49 = 7 x 7
Pythonpython languageprime numberfor looppython function

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com