***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***
***Use tabs for Content***

POSTS

    Calculating a Running Total

    A running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator.

    Programs that calculate the total of a series of numbers typically use two elements:

    • A loop that reads each number in the series.

    • A variable that accumulates the total of the numbers as they are read.

    The variable that is used to accumulate the total of the numbers is called an accumulator. It is often said that the loop keeps a running total because it accumulates the total as it reads each number in the series.

    Example:

    #This program calculates the sum of a series of numbers entered by the user.

     max = 5

     # Initialize an accumulator variable.

     total = 0.0

    print('This program calculates the sum of')

    print(max, 'numbers you will enter.')

    # Get the numbers and accumulate them.

    for counter in range(max):

    number = int(input('Enter a number: '))

    total = total + number

    # Display the total of the numbers.

    print('The total is', total)