Answers for "negative number factor python"

0

negative number factor python

def factorspos(x):
    x = int(x)
    factorspos = []
    print("The factors of",x,"are:")
    if x > 0: # if input is postive
        for i in range(1,x+1):
            if x % i == 0:
                factorspos.append(i)
                print(i)
        return factorspos
    elif x < 0: #if input is negative
        for i in range(x,0):
            if x % i == 0:
                factorspos.append(i)
                print(i)
        return factorspos


print(factorspos(12))   #outputs [1, 2, 3, 4, 6, 12]
print(factorspos(-12))  #outputs [-12, -6, -4, -3, -2, -1]
Posted by: Guest on October-14-2021

Python Answers by Framework

Browse Popular Code Answers by Language