Write a python program to find the sum of the first n natural numbers, where the value of n is provided by the user. What is the sum of the first 200 numbers?
Write a program that finds the average of a series of numbers entered by the user. As in the previous problem, the program will first ask the user how many numbers there are. Note: the average should always be a float. What is the average of 10.2, 1.3, 5.4, 2.2, 5.5, and 14.2?
1. The Python program to find the sum of first n natural numbers with a sample output is provided below :
CODE :
num = int(input("Enter the value of n: "))
item = num
sum = 0
if num <= 0:
print("Enter a whole positive number!")
else:
while num > 0:
sum = sum + num
num = num - 1;
print("Sum of first", item, "natural numbers is: ", sum)
SAMPLE OUTPUT :
2. The Python program that finds the average of a series of numbers entered by the user is provided below :
CODE :
n=int(input("Enter the total number you want to enter:"))
sum=0
for i in range(n):
x=float(input("Enter the number:"))
sum=sum+x
avg=sum/n
print("Average=",avg)
SAMPLE OUTPUT :
If you have any doubt/query regarding the above solution, then let me know in the comment. If the solution helps, kindly give an upVote to the answer.
Get Answers For Free
Most questions answered within 1 hours.