Python Program to Find the Factors of Number

By | October 3, 2021
Python Program to Find the Factors of Number

Python Program to Find the Factors of a Number:

Here in this article, we have written a python code that asks the user to enter a number and print all the factors of that number as an output.

Vamware

Prerequisite topics to create this program.

  • Python Input, Output
  • Python Data types
  • Python User-defined Functions
  • Python Loop

Steps

  • Create a user-defined function factor() that accepts a value as an argument.
  • Create a for loop with a range from 1 to the number n+1.
  • print all the numbers which can divide the function argument.

Python Program to Find the Factors of a Number:

Code:

def factor(num):
    print("The factors of",num,"are:")
    for i in range(1, num + 1):
        if num % i == 0:
            print(i)

num = int(input("Enter a Number: "))
factor(num)

Output 1:

Enter a Number: 200
The factors of 200 are:
1
2
4
5
8
10
20
25
40
50
100
200

Output 2:

Enter a Number: 40
The factors of 40 are:
1
2
4
5
8
10
20
40

People are also reading:

Leave a Reply

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