Computer Science, asked by julie8604, 5 hours ago

PYTHON - Write a function named returnDivisors that accepts a positive integer n as the parameter, and returns a list containing all the divisors of n. Test your function in main. Let user enter an integer from keyboard (your program must be able to check the validation of the entered integer being positive), and then call returnDivisors function which should receive the entered integer as the argument.

Answers

Answered by anindyaadhikari13
1

\texttt{\textsf{\large{\underline{Solution}:}}}

The given co‎‎de is written in Python.

\rule{200}{2}

def returnDivisors(n):

   if n<0:

       return 'Invalid Input'

   return [i for i in range(1,n+1) if n%i==0]

\rule{200}{2}

\texttt{\textsf{\large{\underline{Logic}:}}}

  • Accept the number as input, say N.
  • If N < 0, return error message or else continue.
  • Initialise a list L = [ ]
  • Repeat from I = 1 to N:
  •   If N is divisible by I -> Append 'I' in L.
  • Return L.

See the attachment for output.

Attachments:
Similar questions