Program to display the sum of factors of n which is input by the user
programming language Python
Answers
Answered by
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:
- START
- Ask the user to enter a number.
- Initialise s = 0
- Iterate a loop in the range i = 1 to n.
- Check if the number is a multiple of 'i' or not. If true, add 'i' to s variable.
- Display the value of s.
- STOP.
See the attachment for output ☑.
Attachments:
Similar questions