Questions

How do you Find the Factors of a Number in a while Loop in Python?

In the program given below, we used the while loop to find the factors of a given number. This approach differs when compared to the for loop. Initially, we initialize the variable ‘a’ as 1 before starting the loop. We iterate the while loop till a <a+1. Now we check if the current value is a divisor of z or not. If yes, we print the number and increment the value of ‘a’. If not, we skip the number and continue with the next number. 

                    

def factors(z): 
a = 1 

while(i < z+1): 

if z % a == 0: 

print(a) 

a = a + 1 

numb = int(input("Enter any numb : ")) 

print("The factors for {} are : ".format(numb)) 

factors(numb)

Output:

Enter any numb : 20 

1 

2 

4 

5 

10 

20

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.