Find the Factors of a Number (Exercise)
00:00 Remember when Ian surprised you with a challenge back in the Python Basics course? Well, this is the same challenge, so you might have already solved it, but if you haven’t, here’s a second chance for you to do it before I’m going to walk you over a possible solution.
00:14
So, the challenge is to find the factors of a number, and the text reads, “A factor of a positive integer n
is any positive integer less than or equal to n
, that divides n
with no remainder. For example, 3
is a factor of 12
because 12
divided by 3
is 4
with no remainder.
00:30
However, 5
is not a factor of 12
because 5
goes into 12
twice, and there’s a remainder of 2
.”
00:38
Now your task is to write a program called factors.py
that asks the user to input a positive integer and then prints out the factors of that number. Down there, you can see a sample run of the program with its output. So you can see the user input here, saying "Enter a positive integer: "
.
00:54
The user entered the number 12
, and then you see the output of the program where it says, for every number up to that number, it says 1
is factor of 12
, 2
is a factor of 12
, 3
is a factor of 12
, 4
is, 6
is, and then 12
is a factor of 12
.
01:08
All numbers that 12
can be divided by without leaving a remainder. You already have all the pieces that you need for this in the previous review exercises that we did. So, I hope you have an idea of how to start solving this, and I’d suggest you to go ahead, open up a new file, and then write the program and try it out. Once you’re done, hop over to the next lesson. We’ll code up an example solution together.
Become a Member to join the conversation.