Programming Code Examples

Python Program to Find Factors of a Number1 min read

Write a program to Find Factors of Number in Python


This Python program finds the factors of a given number.

The program defines a function print_factors(x) that takes a number as an argument and prints out all the factors of that number. The function starts by using a for loop to iterate through the range of numbers from 1 to the input number (x+1).

In each iteration, the function checks if the current number (i) is a factor of x by using the modulo operator (%) to check if the remainder of x divided by i is zero. If the condition is true, the current number (i) is printed as a factor of x.

The main program then prompts the user to enter a number and assigns it to the variable num. Then it calls the print_factors(num) function and prints out all the factors of the number entered.

For example, if the user enters the number 45, the output would be:

Add comment