Python Program to Find Factors of a Number

Find factors of a number in python using for loop; In this tutorial, you will learn how to find factors of a given number in python using for loop.

“Factors” are the numbers you multiply to get another number. For instance, factors of 15 are 3 and 5, because 3×5 = 15. Some numbers have more than one factorization (more than one way of being factored). For instance, 12 can be factored as 1×12, 2×6, or 3×4.

Python Program to Find the Factors of a Number using For Loop

Follow the below steps and write a program find factors of a number in python using for loop:

  • Step 1 – Take input number from user
  • Step 2 – Iterate For Loop over every number from 1 to the given number
  • Step 3 – If the loop iterator evenly divides  the provided number i.e. number % i == 0 print it.
  • Step 4 – Print Result
num = int(input("Enter a number: "))

print("The factors of {} are,".format(num))

for i in range(1,num+1):
    if num % i == 0:
        print(i)

After executing the python program, the output will be:

Enter a number: 10
The factors of 10 are,
1
2
5
10

Python Program to Find the Factors of a Number by using Functions

Follow the below steps and write a program find factors of a number in python by using functions:

  • Step 1 – Create Function, which is calculate factors of number
  • Step 2 – Take input number from user
  • Step 3 – Call Function
  • Step 4 – Print Result
# Python Program to find the factors of a number

# This function computes the factor of the argument passed
def print_factors(x):
   print("The factors of",x,"are:")
   for i in range(1, x + 1):
       if x % i == 0:
           print(i)

num = int(input("Enter a number: "))

print_factors(num)

After executing the python program, the output will be:

Enter a number: 10
The factors of 10 are,
1
2
5
10

Recommended Python Programs

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *