Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

factors for negative number 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]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #factors #negative #number #python
ADD COMMENT
Topic
Name
1+4 =