Python Program to Find the Average and Sum of Numbers Until 0 is Entered

Add numbers until 0

Python programs to find the sum and average of the numbers until 0 is entered, are given here. Suppose the input numbers are 5, 10, 9, 0, then the sum = (5 + 10 + 9) = 24 and average = (5 + 10 + 9) / 3 = 8.







1. Algorithm to find the sum and average of the numbers until 0 is entered


1. Set sum = 0, count = 0.

2. Take a number n as input.

3. Perform sum = sum + n.

4. Check if n = 0

5. If step 4 is true declare sum and as the sum and (sum / count) as the average of the numbers entered.

6. If step 4 is false, perform count = count + 1 and go to step 2




2. Pseudocode to find the sum and average of the numbers until 0 is entered


1. Procedure sumAverage():

2. sum := 0

3. count := 0

4. n := Input()

5. Repeat until n = 0

6. sum := sum + n

7. count := count + 1

8. n := Input()

9. avg := (sum / count)

10. Return sum, avg

11. End Procedure





3. Time complexity to find the sum and average of the numbers until 0 is entered


Time Complexity: O(n)

Here n is the number of terms.





4. Program to find the sum and average of the numbers until 0 is entered




4.1. Python Program to find the sum and average of the numbers until 0 is entered using iteration

Code has been copied
# ****************************************
#            alphabetacoder.com
# Python program to find average and sum
# of numbers until 0 is entered
# ****************************************

# initialize
s = 0
count = 0

# keep taking input
# until 0 is entered
n = int(input("Enter a number: "))
while n != 0:
    s = s + n
    count += 1
    n = int(input("Enter a number: "))

# display the result
print("\nTotal number entered: ", count)
print("Sum: ", s)
# display average upto two decimal places
if count == 0:
    print("Average: NaN")
else:
    print("Average: ", round((float(s) / count), 2))

Output


Enter a number: 1

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: 0


Total number entered: 4

Sum: 10

Average: 2.5




4.2. Python Program to find the sum and average of the numbers until 0 is entered using recursion

Code has been copied
# ********************************************
#            alphabetacoder.com
# Python program to find average and sum of
# numbers until 0 is entered using recursion
# ********************************************


def findSumAverage(s, count):
    # take input
    n = int(input("Enter a number: "))

    # keep taking input
    # until 0 is entered
    if n == 0:
        print("\nTotal number entered: ",count)
        print("Sum: ", s)
        if count == 0:
            print("Average: NaN")
        else:
            print("Average: ", round((float(s) / count), 2))
    else:
        # add input to sum and increment count
        s += n
        count += 1
        findSumAverage(s, count)  # recursive call


def main():
    # initialize
    s = 0
    count = 0

    # call recursive function to display result
    findSumAverage(s, count)


# driver code
main()

Output


Enter a number: 10

Enter a number: 5

Enter a number: 8

Enter a number: 12

Enter a number: 7

Enter a number: 0


Total number entered: 5

Sum: 42

Average: 8.4