Write a python program: Print of all divisors of the number

 


Write a program in python, to print divisor of a given number

Steps:

  1. Ask User to enter a Number
  2. Use for loop over 1 to 100 to check divisor
  3. use if loop to find the correct number

#method one
# divisor of number
num=int(input("Enter a number between 1 to 100 : Print divisor of number :"))
for i in range(1,101):
if(num%i==0):
print(i,"is divisor of number ",num)
view raw .py hosted with ❤ by GitHub
Enter a number between 1 to 100 : Print divisor of number :96
1 is divisor of number  96
2 is divisor of number  96
3 is divisor of number  96
4 is divisor of number  96
6 is divisor of number  96
8 is divisor of number  96
12 is divisor of number  96
16 is divisor of number  96
24 is divisor of number  96
32 is divisor of number  96
48 is divisor of number  96
96 is divisor of number  96
Second Method
#Method 2
__author__="Simualtion-hub"
num=int(input("Enter a number between 1 to 100 : Print divisor of number:"))
ListOfDivisor=[]
for i in range(1,101):
if (num%i==0):
ListOfDivisor.append(i)
#printing list
print("The number has following divisors :",ListOfDivisor)
view raw divisor.py hosted with ❤ by GitHub
Enter a number between 1 to 100 : Print divisor of number:96
The number has following divisors:[1, 2, 3, 4, 6, 8, 12,16, 24, 32, 48, 96]

0 Comments