Write a Program using any Programming language that takes a number from user (n) and execute 3 methods..
1: sum of Numbers from 1 to n
2: product of n to 1
3: average of sum from 1 to n
Answers
Answered by
4
The following codes have been written using Python.
#obtaining the number from the user
n = int(input("Enter a number: "))
#calculating the sum
s = 0
for i in range(1, n + 1):
s = s + i
print(s, "is the sum of the numbers from 1 to", str(n) + ".")
#calculating the product
p = 1
for i in range(n, 0, -1):
p = p*i
print(p, "is the product of the numbers from", n, "to 1.")
#calculating the average
l = list()
for i in range(1, n + 1):
l.append(i)
avg = sum(l)/len(l)
print(avg, "is the average of the numbers from 1 to", str(n) + ".")
Similar questions