Check if a number is prime python Code Examples

Last Updated On Friday 21st Jan 2022

A Number that is only divisble by 1 and by itself is called a prime numbers.

prime number program in python

	def checkPrime(number):
    isPrime = False
    if number == 2:
        print(number, 'is a Prime Number')
    if number > 1:
        for i in range(2, number):
            if number % i == 0:
                print(number, 'is not a Prime Number')
                isPrime = False
                break
            else:
                isPrime = True

        if isPrime:
            print(number, 'is a Prime Number')

if __name__ == '__main__':
    userInput = int(input('Enter a number to check: '))
    checkPrime(userInput)

	
	# Enter a number to check: 50
# 50 is not a Prime Number
	

prime factorization

  • program to find out the prime factors of a number.
	def primeFact(n):

  if n==1:
    print('no prime factor for 1')
    return
  lst=[]

  for i in range(2,n):
    while n%i==0:
      lst.append(i)
      n=n//i

  print('Prime Factors of are: {}'.format(lst))
	
	primeFact(12345)
# Prime Factors of are: [3, 5, 823]