Physics and Astronomy
Home Our Teaching Resources C programming Exercises
Contents

Exercise: if()

Exercises form an important part of your learning but are not formally assessed as a part of your course mark. Bring them to a demonstrator next week for feedback.

Extend error checking for the complex root code

Now that we know how to use the if() statement, extend your previous complex root code:

1. Check that the user is not asking for the zeroth root and if so print an error message and return a non-zero value.
2. If the error at the end is unreasonable (say > 1E-12) print an error message and return a non-zero value.

You may either take a copy of your previous project or just extend the original.

Fizz buzz

The game of Fizz buzz involvs a group of children counting 1, 2, 3 etc except that if a number is divisible by three the person says "Fizz" instead of that number, and if it is divisible by five they say "Buzz". If it is divisible by both three and five they say "FizzBuzz".

1. Write a program that reads in an integer from the keyboard and uses an if .. else if statement to print out "Fizz", "Buzz", "FizzBuzz" or the number according to the above rules. Make sure that each printf() statement appears only once.

Hint: You will need to revise the % operator.

NB: the exercise for the switch() statement is deferred to next week for timing reasons.