Exercises on Control Flow

Enhance your programming by practicing loops and conditionals.

The solutions for the exercises are included in the solution tabs. For best learning outcomes, don’t look at these unless you’ve attempted the problems yourself.

Question 1: FizzBuzz

Write a program that prints the numbers from 1 to 100 with the following exceptions:

  • For multiples of 3, print ‘Fizz’ instead of the number.
  • For multiples of 5, print ‘Buzz’ instead of the number.
  • For numbers that are multiples of both 3 and 5, print ‘FizzBuzz’ instead.

Try solving here:

C
#include <stdio.h>
int main(void)
{
// Your code goes here
return 0;
}

Question 2: Newton’s method

Write a program to estimate the square root of 612 using Newton’s method, using five iterations.

Newton's method

Try solving here:

C
#include <stdio.h>
int main(void) {
// Your code goes here
return 0;
}

Question 3: Display triangle

Write a program that displays a triangle with height nn and base width 2n12n−1.

The expected output for n=6n = 6 would be a triangle with height 66 and a base width 1111:

Javascript (babel-node)
*
***
*****
*******
*********
***********

If you’re unsure how to do this, click the “Show Hint” button.

Show Hint

Try solving here:

If you’re stuck, click the “Show Solution” button.

Show Solution

Exercises on Control Flow

Enhance your programming by practicing loops and conditionals.

The solutions for the exercises are included in the solution tabs. For best learning outcomes, don’t look at these unless you’ve attempted the problems yourself.

Question 1: FizzBuzz

Write a program that prints the numbers from 1 to 100 with the following exceptions:

  • For multiples of 3, print ‘Fizz’ instead of the number.
  • For multiples of 5, print ‘Buzz’ instead of the number.
  • For numbers that are multiples of both 3 and 5, print ‘FizzBuzz’ instead.

Try solving here:

C
#include <stdio.h>
int main(void)
{
// Your code goes here
return 0;
}

Question 2: Newton’s method

Write a program to estimate the square root of 612 using Newton’s method, using five iterations.

Newton's method

Try solving here:

C
#include <stdio.h>
int main(void) {
// Your code goes here
return 0;
}

Question 3: Display triangle

Write a program that displays a triangle with height nn and base width 2n12n−1.

The expected output for n=6n = 6 would be a triangle with height 66 and a base width 1111:

Javascript (babel-node)
*
***
*****
*******
*********
***********

If you’re unsure how to do this, click the “Show Hint” button.

Show Hint

Try solving here:

If you’re stuck, click the “Show Solution” button.

Show Solution
C
#include <stdio.h>
int main(void) {
// Your code goes here
return 0;
}