Lecture practice

Writing algorithms

Factorials, loops and formulas. Three worked examples, then a team game.

Monther Ibrahime

→ next ← back F full screen

Example 1 of 3

Write an algorithm to calculate the factorial of any number N the user enters.

Test your answer: when N = 5 it should print 120.

Show a hint

Keep a counter i that goes 1, 2, 3 … up to N, and multiply fact by i each time.

Example 2 of 3

Write an algorithm that calculates

y = 4! × 5!

Test your answer: it should print 2880.

Show a hint

You only need one loop. Once you have 4!, notice that 5! = 5 × 4!.

Example 3 of 3

Write an algorithm to calculate

y = 4X! + Z²

The user enters X and Z. Test your answer: X = 2 and Z = 3 should print 17.

Show a hint

Work out X! with a loop first. Then put it into the formula.

End of lecture game

Factorial Face‑off

  • 12 questions about today's three algorithms.
  • Teams take turns. Each turn has 30 seconds.
  • A right answer scores 100 points, plus up to 50 for speed.
  • Miss it, and the next team can steal it for 50.

How many teams?

Writing algorithms

1 / 8