Factors of a Number Program in Python

Hi, Programmer welcome to the program of factors of a Number in Python computer programming language. This project is very useful for mid-level programmers and BTech Engineering students.

Theory: Factors of a Number Program in Python

A number which divides the other given number completely ( gives remainder as 0).

Step by Step Guide: Factors of a Number Program in Python

1. Library file of module math and sys is imported to the program for the work of math for calculating square root and sys for exiting the project.
2. Main program of the project is run with the use of while loop. Numbers can be given in the computer again and again, for quit of the project quit is input in place of a number.
3. Check is made whether the input number is greater than 0 and is in decimal format. Program is continued if the condition is true.
4. Finding the factor of a number check is made whether the given number divided by the other number gives zero as remainder.

Source code :

"""Factor Finder, by Archit
  This program shows the factors of a user given number.
  """
 
import math, sys

print('''Factor Finder, by Archit
    A number when divided by other number gives remainder 0.
 ''')

while True: # Main program while loop.
    print('Enter a Natural number to factor (or QUIT):')
    inputnumber = input('> ')
    if inputnumber.upper() == 'QUIT':
        sys.exit()

    if not (inputnumber.isdecimal() and int(inputnumber) > 0):
        continue
    number = int(inputnumber)

    factors = []

    # Finding the factors of number:
    for i in range(1, int(math.sqrt(number)) + 1):
        if number % i == 0: # If there's no remainder means equal to zero,then it is a factor.
            factors.append(i)
            factors.append(number // i)

      # Converting to a set to get rid of duplicate factors
    factors = list(set(factors))
    factors.sort()

     # Displaying the results:
    for i, factor in enumerate(factors):
        factors[i] = str(factor)
    print(', '.join(factors))

Output:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license()” for more information.

==== RESTART: C:/Users/ARCHIT/Desktop/new python progrm/factor finder.py ====
Factor Finder, by Archit
A number when divided by other number gives remainder 0.

Enter a Natural number to factor (or QUIT):

56
1, 2, 4, 7, 8, 14, 28, 56
Enter a Natural number to factor (or QUIT):
52
1, 2, 4, 13, 26, 52
Enter a Natural number to factor (or QUIT):
12
1, 2, 3, 4, 6, 12
Enter a Natural number to factor (or QUIT):
23
1, 23
Enter a Natural number to factor (or QUIT):
quit

Conclusion :

I have input three number at different time. The output of the program shows the program is running fine, as the result given is matching the number of factors. Hence the program is running fine.

Thank you,fellow programmers for reading and copying the project on Factors of a Number Program in Python. Click the link below for reading, downloading and copying the programs necessary for the programmer life.
Python Programs Archives – CS Study

Leave a Comment

Your email address will not be published.