Design a Pythonprogram

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter3: Understanding Structure
Section: Chapter Questions
Problem 20RQ
icon
Related questions
Question

Design a Pythonprogram to prime the sum of all the even numbers from 1 to 20.

Expert Solution
Step 1

Programming approach:

  • Set the lower limit to 1.
  • Set the upper limit to 20.
  • Set total to 0.
  • Iterate each number in the list from lower to upper limit using for loop:
    • Check if the number is even then
      • Print Number.
      • Update total by adding the previous value of the total to the current number.
  • Print total.
Step 2

The following python program that prints the sum of all the even numbers from 1 to 20:

#set lower limit to 1
low = 1
#set higher limit to 20
high = 20
#set total to 0
total = 0
# iterate each number in list 
for number in range(low, high + 1): 
    #check if number is even
    if number % 2 == 0: 
        print(number) 
        
        #update total for every number
        total = total + number

print("The sum of even numbers from 1 to 20 is",total)

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Top down approach design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage