Python Program to Find the Factors of Number

Posted in

Python Program to Find the Factors of Number
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    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.

    Prerequisite Python Program to Find the Factors of Number

    • 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

    Python 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 Comment on this Post

    0 Comments