menu-button

Control Flow & Loops

Task #1a: Printing 1 to 20

Write a program, making use of a for loop, that prints the numbers 1 to 20 on the console.

Reset to Original Code     Fullscreen

Task #1b: Printing sum of 1 to 50

Write a program, making use of a for loop, that adds all the whole numbers between 1 and 50. You will need to declare a variable, e.g. total, which keeps a running total.

Reset to Original Code     Fullscreen

Task #1c: Sum of even numbers between 1 and 50

Write a program that adds all the even numbers between 1 and 50.

Reset to Original Code     Fullscreen

Task #2a: Outputting multiples of 3

Write a suitable program which outputs all the multiples of 3 up to 50 to the console. Use either a while or for loop.

Reset to Original Code     Fullscreen

Task #2b: Outputting 1 more than a multiple of 4

Write a suitable program which outputs all the numbers between 1 and 100 that are 1 more than a multiple of 4; your sequence should start 1, 5, 9, ....

Reset to Original Code     Fullscreen

Task #3a: Factors of 100

Write a suitable program which works out and outputs all the factors of 100. You may want to consider each number up to 100 and think of a way to determine if it's a factor of 100. Use either a for loop.

Reset to Original Code     Fullscreen

Task #3b: Factors of any number

Write a modified form of your programme above that inputs a number, e.g. using prompt(...), and finds factors of this number. You may want to consider how to make your programme more efficient.

Reset to Original Code     Fullscreen

Task #4a: Keep Halving!

Write a function that inputs a positive whole number n, and keeps halving it until you get an odd number, which you should return. You should make use of a while loop.

Reset to Original Code     Fullscreen

Task #4b: Collatz Fun

Write a function that uses a while loop to achieve the following:

  1. Start with the number n that has been input via the prompt command.
  2. If the number is even, half it. If it is odd, multiply it by 3 and add 1.
  3. Repeat the above step until you reach 1.
Your program should output all the numbers on the way to 1. For example, if the user input 20, your programme should output: 20, 10, 5, 16, 8, 4, 2, 1.

Reset to Original Code     Fullscreen

Task #5: Outputting prime numbers

Write a suitable program which outputs all the prime numbers between 1 and 300. You will need a way to determine whether each number you consider is prime.

Reset to Original Code     Fullscreen

Challenge: Highest Common Factor

The Euclidean Algorithm is a well known technique to find the Higher Common Factor of two numbers. For two numbers x and y:

  1. If y is 0 then stop and output x, and vice versa.
  2. Find the remainder when x is divided by y. For example, when 30 is divided by 4, the quotient is 7 and the remainder is 2.
  3. Make x equal to y and y equal to this remainder, then go back to Step 1.
Reset to Original Code     Fullscreen