Questions
A primary school teacher wants a program to test their pupils' knowledge of times tables (2 - 12). Read through the code for this problem and answer the following questions.
- Why is it a good idea to use a constant for the number of questions being asked?
- How are constants different from variables?
- Explain, with reference to the code, how a random number is being generated.
- The program uses a loop (see chapter 6). Can you explain how this loop works?
- Explain the line
correct++
- Modify the code to output the percentage of correct answers
- Explain what is meant by exception handling, how and where might exception handling be used in this program?
Additional Exercises
- Using a bitwise operator write a program that checks if an integer is odd or even.
- The gravitational field of the Moon is approximately 17% that of the Earth. Write a program that calculates your weight on the Moon.
- Write a program that takes as input a four digit number and calculates the sum of the digits
- EXTENSION: Write a program that checks if a given number input from the keyboard is a prime number. (This requires a loop.)