Skip to content

Instantly share code, notes, and snippets.

@sshehrozali
Created December 15, 2020 12:41
Show Gist options
  • Save sshehrozali/91329d1d953f2a1fadc9246083dc5493 to your computer and use it in GitHub Desktop.
Save sshehrozali/91329d1d953f2a1fadc9246083dc5493 to your computer and use it in GitHub Desktop.
Python function to print all numbers that are divisible by 2 or 3
# Function to print divisibles of 2 or 3
def even(n):
if n == 2:
print(n)
exit(0)
# Initiliaze counters
prime_counter = 0
check = 0
counter = 1
# Loop to check number of prime numbers till n
for i in range(2, n):
if i % 2 != 0 and i % 3 != 0:
prime_counter = prime_counter + 1
check = n - 1 - prime_counter
print(prime_counter)
# Loop to print n numbers that are divisible by 2 or 3
for j in range(2, n):
if j % 2 == 0 or j % 3 == 0:
# Update counter
counter = counter + 1
# If no prime numbers are found
if prime_counter == 0:
# If not last number, seperate it with ','
if counter != n - 1:
print(j, ",", end="")
# If last number, end with new-line
if counter == n - 1:
print(j)
# If prime numbers are found
if prime_counter != 0:
# If not last number, seperate it with ','
if counter != check:
print(j, ",", end="")
# If last number, end with new-line
if counter == check:
print(j)
# MAIN PROGRAM #
# Take input from user and call even()
no = abs(int(input("\nEnter Any Number: ")))
even(no)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment