Skip to content
ExamSpine

Lesson 3.6 · Write it

Sum a range

A whole programPython or Java6 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads two whole numbers, a start and an end, and prints the sum of every number from the start to the end, inclusive.

  1. Read the start on the first line and the end on the second line.
  2. Add every number from the start to the end, including both.
  3. Print Sum: followed by the total.

The start is always less than or equal to the end. Both are whole numbers from 1 to 1000. Your program is run several times. Only what it prints is checked.

In Python, remember that the end value of range is excluded, so to include the end, go one past it.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • 1 + 2 + 3 + 4 + 5 = 15 (rule 2)

    You type

    1. typed: 1
    2. typed: 5

    It prints

    Sum: 15
  • start equals end, sum is just that number (rule 2)

    You type

    1. typed: 3
    2. typed: 3

    It prints

    Sum: 3
  • 2 + 3 + 4 = 9 (rule 2)

    You type

    1. typed: 2
    2. typed: 4

    It prints

    Sum: 9
solution.pySolution.java