Skip to content
wannacode

How to solve a task

Problem

The blank screen

You have a task: on the first day you save €1, on the second €2, on the third €3, and so on. How many euros will you have after N days?

You open the editor and… you do not know which line to start with. This happens to everyone, and the reason is rarely that you do not know the language. Usually it is not yet clear what exactly the program has to do. That is why the solution starts before the code: on paper or in your head.

The idea

Read the task

The tasks in this course look like this:

Savings. On the first day you save €1, on the second €2, on the third €3, and so on. The input is the number of days N. Print how many euros you have saved in total.

Input: 5 Output: 15

Before you think about a solution, answer three questions:

  1. What do I get? One whole number N, the number of days.
  2. What do I have to print, and in what form? One number, with no other text.
  3. Do I understand the example? Work it out by hand: 1 + 2 + 3 + 4 + 5 = 15. It matches the output.

If your calculation by hand does not give the output from the example, you have not understood the task. Read it again before you write anything.

The idea

Split the task

A big task is hard to solve all at once. Split it into parts, each of which you know how to do:

The parts
1. read the number of days N
2. add up the numbers from 1 to N
3. print the total

The first and third parts are clear. The second one is still big, so split it too. How do you add things up by hand? You keep the total in your head and add the next number to it, one at a time. That is what a cashier does when ringing up your shopping.

Keep splitting until each part is so small that it is clear how to do it.

Next to the animation is the program that follows these parts. You will learn the words in it in the next module. For now, watch how each part turns into a line or two of code.

Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
int sum = 0;
for (int day = 1; day <= n; day++) {
sum += day;
}
System.out.println(sum);
Step by step, in words
  1. All at once, the task is big: how many euros do you save in N days?
  2. You split it into three parts. Each one is clear on its own.
  3. Part 1: read N. In the example, N = 5.
  4. Part 2: the total starts at 0, and each number from 1 to N is added to it.
  5. Dry run: 0 + 1 + 2 + 3 + 4 + 5 = 15.
  6. Part 3: print the total. The output is 15, just like in the example.

The idea

Write the plan in words

Now put the steps in order, in plain words:

The plan
read N
sum = 0
for each number from 1 to N:
add the number to the sum
print the sum

This is called pseudocode: a description of the solution that looks like a program but is not in any language. There are no strict rules. What matters is that each step is clear and the steps are in the right order.

The indent on the fourth line shows that this step repeats for each number. Soon you will see how to write that in a real language.

See it

Dry run

Does the plan work? Check it before there is any code. Take the value from the example (N = 5) and go through the steps by hand, writing down the sum after each one:

Step Number Sum before Sum after
start 0
1 1 0 1
2 2 1 3
3 3 3 6
4 4 6 10
5 5 10 15

The last step prints 15, exactly as in the example. The plan is right, at least for this case.

This is called a dry run: you go through the solution with sample values without running it. It takes a few minutes, and it catches logic errors before you have written a single line of code.

More precisely, if you are curious
  • There is another way to get the same sum: N × (N + 1) / 2. For N = 5 that is 5 × 6 / 2 = 15. Two different plans give the same result. The second one is faster, but to come up with it you have to know the formula. The first one follows exactly how you would work it out by hand.
  • You will follow values line by line like this with real code too. Editors have a tool for it, the debugger: it stops the program on a line you choose and shows the values of everything in it.

Try it

Find the bug

For N = 5 this plan gives 16 instead of 15. Do a dry run in your head. Which line is the bug on?

Pick the line with the bug.

The idea

Check the solution against the example

Once you have a program, the check is the same, except that you run it instead of working it out by hand:

  1. Enter the input from the example and compare your output with the one in the task. Compare character by character: "The sum is 15" is not "15". Solutions are often checked automatically, and extra text makes the answer wrong.
  2. Try your own values too, especially edge cases: the smallest allowed value, zero, a very large number.
  3. If the output does not match, do a dry run of your code with the sample value. The place where the values stop matching what you expect is the bug.

Try it with the savings plan: what does it give for N = 1? And for N = 0?

The answer
  • For N = 1 the step repeats once: the sum becomes 0 + 1 = 1. That is right: after one day you have €1.
  • For N = 0 there are no numbers from 1 to 0, so the step does not run even once. The sum stays 0. That is right too: in zero days you have saved nothing.

Test

Check yourself

1. What is pseudocode?
2. What do you do in a dry run?
3. The task says: input 3, output 6. Your solution prints "The sum is 6". Is that right?
4. The plan adds up the numbers from 1 to N, starting from sum = 0. What is the sum after the third step?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·