Computer Science, asked by paz90nnbnskh, 3 months ago

Program to display the sum of factors of n which is input by the user


programming language Python​

Answers

Answered by anindyaadhikari13
4

Required Answer:-

Question:

  • Write a Python program to display the sum of factors of a number.

Solution:

Here comes the códe.

Approach 1.

n,s=int(input("Enter a number: ")),0

for i in range(1,n+1):

if n%i==0:

s+=i

print("Sum of factors: ",s)

Approach 2. This is quite short as compared to the previous approach.

n=int(input("Enter a number: "))

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

print("Sum of factors: ",s)

Algorithm:

  1. START
  2. Ask the user to enter a number.
  3. Initialise s = 0
  4. Iterate a loop in the range i = 1 to n.
  5. Check if the number is a multiple of 'i' or not. If true, add 'i' to s variable.
  6. Display the value of s.
  7. STOP.

See the attachment for output ☑.

Attachments:
Similar questions